-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
dataclasses.Field.default and .default_factory can be _MISSING_TYPE #5900
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
Conversation
This comment has been minimized.
This comment has been minimized.
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.
Thanks, but please use the new union syntax (i.e. Foo | Bar
).
I considered doing this earlier, but didn't because I think this will be slightly annoying, since the idiomatic way to narrow is There probably isn't a good solution here, but just wanted to call it out. |
Yes, I had the same concern as Shantanu. I think PEP 661 (https://www.python.org/dev/peps/pep-0661/) would provide a cleaner solution. |
Maybe @altendky has a use case that requires this change? We can always change over to PEP 661 when that gets accepted and is available for use. That said, I have no opinion, what solution is best. |
We could define Edit: PEP 661 mentions this as a rejected idea, but unlike most of their other rejected ideas, it actually works and it was apparently rejected only because nobody likes it. |
Welp, of course the time I don't chat about a change first it ends up deserving discussion. if field.default_factory != dataclasses.MISSING: # type: ignore[misc]
Here's a minimal example. Note that the first error seems to be python/mypy#10750 and I was already avoiding it with https://mypy-play.net/?mypy=latest&python=3.10&flags=strict&gist=f66bb887158648e99b97c042165dd684 import dataclasses
@dataclasses.dataclass
class C:
f: int = 0
for field in dataclasses.fields(C):
if field.default_factory != dataclasses.MISSING:
pass
I just tried Regardless, I'll be adding an ignore "for now" since of course this wouldn't be released anyways. If there's another more proper solution and this isn't a useful interrim change, I can handle that. Thanks to all for the interest and effort here. |
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: pydantic (https://github.com/samuelcolvin/pydantic.git)
- pydantic/dataclasses.py:174: error: unused "type: ignore" comment
|
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.
This looks fine to me, but I'll defer judgement to @hauntsaninja and @JelleZijlstra.
I'd prefer to stick with the current stub until PEP 661 gives us better tools to deal with this situation. |
No description provided.