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

Return property mapping on get #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/enum_prop/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def __set_name__(self, owner: type[Enum], name: str) -> None:

def __get__(self, instance: Enum, owner: type[Enum]) -> V:
assert self.bound_name is not None
if instance is None:
return self
try:
return self.mapping[instance]
except KeyError as e:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ class B(enum.Enum):

with pytest.raises(AttributeError, match=r"B.b has no attribute 'val'"):
B.b.val

def test_getting_enum_property() -> None:
Comment on lines +32 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def test_getting_enum_property() -> None:
def test_getting_enum_property() -> None:

I think black will complain here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def test_getting_enum_property() -> None:
def test_can_introspect_property_attributes() -> None:

Let's name the test so that the aspect we're testing is clear.

class C(enum.Enum):
a = "a"
b = "b"
val = enum_property({a: 1, b: 2})

assert C.val
assert C.val.bound_name is "val"