-
Notifications
You must be signed in to change notification settings - Fork 106
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
Feature/handle generics #209
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
71a093d
Add basic handling of typing.Generic
mciszczon 01774eb
Handle older Python versions
mciszczon 53f2f32
Improve performance
mciszczon 5d0efb5
Cache issubclass call
mciszczon 9218ebc
Do not fail CI job if performance alert
mciszczon 912b13e
Do not fail on master CI
mciszczon c42b862
Merge branch 'master' into feature/handle-generics
mciszczon 2273cea
Remove accessing dotted attributes in data
mciszczon a34ab8a
Fix mypy error for safe import
mciszczon f298260
Add more c heck to default factory identity test
mciszczon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'm using this branch for a project and found this. This block doesn't make sense and also doesn't work.
get_type_hints
gets a mapping from field name to field type for the dataclassvalue
. You're iterating over that, but you only ever check the first item because both branches end withreturn
. I'm pretty sure both of those returns in the loop should only be returning if the value is False.On the first branch, if
field_type
is a TypeVar, you check the field value against all the arguments for the subscripted generictype_
. Why isget_args
in the loop? Why isgetattr
in the inner loop? Neither one is changing at that point. I believe this would also fail to detect an incorrect type if, say, a field is hintedAnyStr
, the value is None, and the dataclass has two type arguments, onestr
and oneNone
.I assume the second branch is trying to check if the field value matches the field type? But what you're actually doing is checking if the dataclass matches the subscripted dataclass type. This crashes with the exception
TypeError: Subscripted generics cannot be used with class and instance checks
for obvious reasons.For the false negative issue with multiple type arguments, possibly something could be done with
__orig_bases__
? Not sure.Here's my suggestion for a fixed version. I haven't written test cases, but I did try it with my fairly complicated use case and it seems to work.