Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value for target_root, to make recipe modules importable #588

Merged
merged 6 commits into from
Sep 1, 2023

Conversation

cisaacstern
Copy link
Member

Closes #587

@@ -437,6 +438,8 @@ class WriteCombinedReference(beam.PTransform, ZarrWriterMixin):
:param output_json_fname: Name to give the output references file. Must end in ``.json``.
"""

store_name: str
target_root: Union[str, FSSpecTarget] = field(default_factory=os.getcwd)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmmmm, I'm not a fan of getcwd as now behavior fundamentally differs based on where we're running this from. This leads to confusing to debug stuff...

I made a different suggestion for fixing this in #587 (comment).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

What about a sentinel value (based on this SO and this PEP), something like:

class InjectableDefault:
   """Serves as a default for values which we expect to be either set or injected."""
   pass

def injectable_default():
    return InjectableDefault()

@dataclass
class StoreToZarr(...):
    ...
    target_root: Union[str, FSSpecTarget, InjectableDefault] = field(default_factory=injectable_default)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that possibly make debugging clearer because it just won't work?

Trying to see if we can add this as a small patch without dropping a whole Python version support...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A possible benefit of this InjectableDefault approach is that it provides a type hint in the code which can flag a given argument as also being declared in #566 (once that lands)? So when we read the transforms module... we know what we expect to be injected, without cross-referencing to the injection spec added by that PR...?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, we should just drop the python version support! If we want to do something with types, we can do that separately from this - but IMO in this case, the right way to tackle this is to drop 3.9 support.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO in this case, the right way to tackle this is to drop 3.9 support.

Just realizing that the only thing dropping 3.9 solves is where we set the default, not what the default is.

With 3.10, we can set the default on ZarrWriterMixin and still have required fields on child classes. With 3.9, we need to set the default on the child classes.

But in either case, we need to chose what the default is. 5a37cb7 demonstrates the sentinel class idea (again, this could be thought of as agnostic to 3.9 or 3.10, we need a default either way).

I think the empty sentinel works nicely, and indeed prevents the recipe from being run in the first place.

What if we use something like this here, so we can just merge this, and drop 3.9 in a more deliberate PR?

Copy link
Contributor

@yuvipanda yuvipanda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call it RequiredAtRuntimeDefault or something of that sort?

pangeo_forge_recipes/transforms.py Outdated Show resolved Hide resolved
Co-authored-by: Yuvi Panda <yuvipanda@gmail.com>
@cisaacstern
Copy link
Member Author

cisaacstern commented Aug 31, 2023

Let's call it RequiredAtRuntimeDefault or something of that sort?

Love it... updating now...

Edit: Will finish this later, need to go have dinner 😄

@cisaacstern
Copy link
Member Author

cisaacstern commented Aug 31, 2023

@yuvipanda how does this look now?

@cisaacstern cisaacstern added the test-integration Apply this label to run integration tests on a PR. label Sep 1, 2023
@cisaacstern
Copy link
Member Author

Going to merge:

✅ All checks passing
✅ All review items addressed

🙏 Thanks @yuvipanda! This is a lot better thanks to your feedback.

@cisaacstern cisaacstern merged commit f4202a6 into main Sep 1, 2023
23 checks passed
@cisaacstern cisaacstern deleted the target-root-default branch September 1, 2023 04:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test-integration Apply this label to run integration tests on a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recipe modules with injectable kws should still be importable (use defaults?)
2 participants