Support if statements in dataclass_transform class#14854
Support if statements in dataclass_transform class#14854sobolevn merged 6 commits intopython:masterfrom KRunchPL:fix-if-in-dataclass-transform-class
Conversation
This comment has been minimized.
This comment has been minimized.
sobolevn
left a comment
There was a problem hiding this comment.
Thanks! The usecase with if TYPE_CHECKING seems reasonable to me.
I personally use this from time to time.
| present_2: int | ||
| if not False: | ||
| present_3: int | ||
| if not TRUTH: |
There was a problem hiding this comment.
Let's add at least a single test with some def cond() -> bool: ... function.
if cond():
x: int
y: int
z1: int
else:
x: str
y: int
z2: intThere was a problem hiding this comment.
I have added the test. It is of course expecting errors, because mypy cannot know what is the return value of a function right? The condition logic is based on IfStmt.is_unreachable which is filled in by logic found in mypy.reachability. I have added a test with some comments that not only tests, but also shows the behavior. I am not sure if we can avoid this limitation (although I am new here, so I can be mistaken). And of course most typical use case will be if TYPE_CHECKING, which we can know is ALWAYS_TRUE.
There was a problem hiding this comment.
Yes, I think the test is correct 👍
sobolevn
left a comment
There was a problem hiding this comment.
One last thing from me: as far as I understand - this does also affect how @dataclasses are defined.
Can we please add a single test for it as well?
Thanks a lot for your work! 👍
This comment has been minimized.
This comment has been minimized.
|
Added the test for pute dataclass. |
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
|
Thank you! |
Fixes: #14853
Adding support for
ifstatements in the dataclass_transform class, so the attributes defined conditionally are treated as those that are directly in class body.