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
make match() on Enum into a top-level function in meta.py #8504
make match() on Enum into a top-level function in meta.py #8504
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Thank you Danny and John!
tests/python/pants_test/backend/python/tasks/native/test_ctypes_integration.py
Outdated
Show resolved
Hide resolved
@@ -18,7 +20,7 @@ def current(cls) -> 'Platform': | |||
|
|||
@memoized_property | |||
def runtime_lib_path_env_var(self): | |||
return self.match({ | |||
return match(self, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess, but it seems much more straight forward to say:
class Platform(Enum):
darwin = ("darwin", "DYLD_LIBRARY_PATH")
linux = ("linux", "LD_LIBRARY_PATH")
def __new__(cls, value, runtime_lib_path_env_var) -> 'Platform':
instance = super().__new__(cls, value)
instance._runtime_lib_path_env_var = runtime_lib_path_env_var
return instance
@property
def runtime_lib_path_env_var(self):
return self._runtime_lib_path_env_var
Or even:
@dataclass(frozen=True)
class PlatformInfo:
name: str
runtime_lib_path_env_var: str
class Platform(Enum):
darwin = PlatformInfo("darwin", "DYLD_LIBRARY_PATH")
linux = PlatformInfo("linux", "LD_LIBRARY_PATH")
# Optional for less code ripple, but arguably you'd just kill and have users say:
# `platform_instance.value.runtime_lib_path_env_var` themselves.
@property
def runtime_lib_path_env_var(self):
return self.value.runtime_lib_path_env_var
IOW if the enum value passed to match is self
, that seems like a smell that the enum value should be a struct instead of a single value. If you agree I'd definitely follow up and not scope creep this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree and will move to a followup PR!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay! Thanks!
f47baec
to
28f0f9b
Compare
Hello, was this previously blocked by the broken master CI? Would love to see this land :) |
2441636
to
4e99376
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very neat solution, thanks!
7e3b356
to
4e5e7a3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay! Thank you for finishing this out.
Nit: consider from pants.util.enums import match
and using match
directly. This fits with the codebase style better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Thanks Danny!
71ee8d4
to
544d260
Compare
544d260
to
64a9d32
Compare
Problem
Importing via
from pants.util.collections import Enum
induces an indirection which isn't necessary for most users ofEnum
(the ones that don't use the.match()
function).Solution
match
into a top-level function inmeta.py
.Result
An awkward import is resolved, and a new import is born! Complete credit for the solution goes to @jsirois in the
#engine
channel on our Slack.