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

IsEnum #69

Closed
wants to merge 8 commits into from
Closed

IsEnum #69

wants to merge 8 commits into from

Conversation

FBruzzesi
Copy link
Contributor

@FBruzzesi FBruzzesi commented Apr 30, 2023

In a similar fashion to #68, it introduces three types to check enum's:

  • IsEnumType: checks if a class definition is subclass of Enum, and possibly checks exact matching of member values using IsStrictDict.
  • IsPartialEnumType: checks if a class definition is subclass of Enum, and possibly checks partial matching of member values using IsPartialDict.
  • IsEnum: checks if an instance is from Enum or subclass (arguably this is the most "WIP", since I think it should be possible to check if a value is part of an enum members)

I am tagging this as WIP as we didn't discuss about it.
(Worked on this as it is in the list of #3 )

@codecov
Copy link

codecov bot commented Apr 30, 2023

Codecov Report

Merging #69 (5d665aa) into main (77ddc33) will decrease coverage by 0.23%.
The diff coverage is 94.59%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #69      +/-   ##
==========================================
- Coverage   99.75%   99.52%   -0.23%     
==========================================
  Files          12       12              
  Lines         813      850      +37     
  Branches      146      151       +5     
==========================================
+ Hits          811      846      +35     
- Misses          2        3       +1     
- Partials        0        1       +1     
Impacted Files Coverage Δ
dirty_equals/__init__.py 100.00% <ø> (ø)
dirty_equals/_other.py 98.02% <94.59%> (-0.77%) ⬇️

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 77ddc33...5d665aa. Read the comment docs.

self._enum_cls = enum_cls

def equals(self, other: Any) -> bool:
return isinstance(other, self._enum_cls)
Copy link
Contributor Author

@FBruzzesi FBruzzesi Apr 30, 2023

Choose a reason for hiding this comment

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

One idea to account for checking if other is a value in the _enum_cls could be:

if isinstance(other, Enum):
    return isinstance(other, self._enum_cls)
else:
    return other in {i.value for i in self._enum_cls}

Leading to:

from enum import Enum, auto

class ExampleEnum(Enum):
    a = auto()
    b = auto()

assert 1 == IsEnum(ExampleEnum)
assert 3 != IsEnum(ExampleEnum)

@FBruzzesi FBruzzesi changed the title WIP: IsEnum IsEnum May 1, 2023
@FBruzzesi
Copy link
Contributor Author

FBruzzesi commented May 1, 2023

Adjusted with feedbacks from #68, still think that comment for IsEnum is relevant and worth discussing.

@FBruzzesi FBruzzesi closed this by deleting the head repository Aug 7, 2023
@FBruzzesi
Copy link
Contributor Author

FBruzzesi commented Aug 24, 2023

Hi @samuelcolvin, I completely forgot about this PR and deleted the fork.
Not sure if you have the time to look into it and/or interested in having such feature

@samuelcolvin
Copy link
Owner

Hi, sorry for the slow reply, life has been fairly busy.

Yes, I think it's worth adding. Feel free to create a new PR.

@FBruzzesi FBruzzesi mentioned this pull request Aug 24, 2023
@FBruzzesi
Copy link
Contributor Author

Hi, sorry for the slow reply,

Absolutely no need to be sorry, I should have been the one not deleting the PR 🙈

life has been fairly busy.

I can barely imagine!

Yes, I think it's worth adding. Feel free to create a new PR.

samuelcolvin pushed a commit that referenced this pull request Sep 15, 2023
Remark: This is a replica of #69, which I accidentally deleted.

# Description

In a similar fashion to #68, it introduces four types to check enum's:

- `IsEnumType`: checks if a class definition is subclass of `Enum`, and
possibly checks exact matching of member values using `IsStrictDict`.
- `IsPartialEnumType`: checks if a class definition is subclass of
`Enum`, and possibly checks partial matching of member values using
`IsPartialDict`.
- `IsEnum`: checks if an instance is from `Enum` or subclass (arguably
this is the most "WIP", since I think it should be possible to check if
a value is part of an enum members)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants