-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Support if statements in dataclass_transform class #14854
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
Changes from 1 commit
c832d38
09e77ef
c78dc9c
c41f3fb
84daa9b
f1b1642
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -451,3 +451,187 @@ Foo(1) # E: Too many arguments for "Foo" | |
|
||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/dataclasses.pyi] | ||
|
||
[case testDataclassTransformTypeCheckingInFunction] | ||
# flags: --python-version 3.11 | ||
from typing import dataclass_transform, Type, TYPE_CHECKING | ||
|
||
@dataclass_transform() | ||
def model(cls: Type) -> Type: | ||
return cls | ||
|
||
@model | ||
class FunctionModel: | ||
if TYPE_CHECKING: | ||
string_: str | ||
integer_: int | ||
else: | ||
string_: tuple | ||
integer_: tuple | ||
|
||
FunctionModel(string_="abc", integer_=1) | ||
FunctionModel(string_="abc", integer_=tuple()) # E: Argument "integer_" to "FunctionModel" has incompatible type "Tuple[<nothing>, ...]"; expected "int" | ||
|
||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/dataclasses.pyi] | ||
|
||
[case testDataclassTransformNegatedTypeCheckingInFunction] | ||
# flags: --python-version 3.11 | ||
from typing import dataclass_transform, Type, TYPE_CHECKING | ||
|
||
@dataclass_transform() | ||
def model(cls: Type) -> Type: | ||
return cls | ||
|
||
@model | ||
class FunctionModel: | ||
if not TYPE_CHECKING: | ||
string_: tuple | ||
integer_: tuple | ||
else: | ||
string_: str | ||
integer_: int | ||
|
||
FunctionModel(string_="abc", integer_=1) | ||
FunctionModel(string_="abc", integer_=tuple()) # E: Argument "integer_" to "FunctionModel" has incompatible type "Tuple[<nothing>, ...]"; expected "int" | ||
|
||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/dataclasses.pyi] | ||
|
||
|
||
[case testDataclassTransformTypeCheckingInBaseClass] | ||
# flags: --python-version 3.11 | ||
from typing import dataclass_transform, Type, TYPE_CHECKING | ||
|
||
@dataclass_transform() | ||
class ModelBase: | ||
... | ||
|
||
class BaseClassModel(ModelBase): | ||
if TYPE_CHECKING: | ||
string_: str | ||
integer_: int | ||
else: | ||
string_: tuple | ||
integer_: tuple | ||
|
||
BaseClassModel(string_="abc", integer_=1) | ||
BaseClassModel(string_="abc", integer_=tuple()) # E: Argument "integer_" to "BaseClassModel" has incompatible type "Tuple[<nothing>, ...]"; expected "int" | ||
|
||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/dataclasses.pyi] | ||
|
||
[case testDataclassTransformNegatedTypeCheckingInBaseClass] | ||
# flags: --python-version 3.11 | ||
from typing import dataclass_transform, Type, TYPE_CHECKING | ||
|
||
@dataclass_transform() | ||
class ModelBase: | ||
... | ||
|
||
class BaseClassModel(ModelBase): | ||
if not TYPE_CHECKING: | ||
string_: tuple | ||
integer_: tuple | ||
else: | ||
string_: str | ||
integer_: int | ||
|
||
BaseClassModel(string_="abc", integer_=1) | ||
BaseClassModel(string_="abc", integer_=tuple()) # E: Argument "integer_" to "BaseClassModel" has incompatible type "Tuple[<nothing>, ...]"; expected "int" | ||
|
||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/dataclasses.pyi] | ||
|
||
[case testDataclassTransformTypeCheckingInMetaClass] | ||
# flags: --python-version 3.11 | ||
from typing import dataclass_transform, Type, TYPE_CHECKING | ||
|
||
@dataclass_transform() | ||
class ModelMeta(type): | ||
... | ||
|
||
class ModelBaseWithMeta(metaclass=ModelMeta): | ||
... | ||
|
||
class MetaClassModel(ModelBaseWithMeta): | ||
if TYPE_CHECKING: | ||
string_: str | ||
integer_: int | ||
else: | ||
string_: tuple | ||
integer_: tuple | ||
|
||
MetaClassModel(string_="abc", integer_=1) | ||
MetaClassModel(string_="abc", integer_=tuple()) # E: Argument "integer_" to "MetaClassModel" has incompatible type "Tuple[<nothing>, ...]"; expected "int" | ||
|
||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/dataclasses.pyi] | ||
|
||
[case testDataclassTransformNegatedTypeCheckingInMetaClass] | ||
# flags: --python-version 3.11 | ||
from typing import dataclass_transform, Type, TYPE_CHECKING | ||
|
||
@dataclass_transform() | ||
class ModelMeta(type): | ||
... | ||
|
||
class ModelBaseWithMeta(metaclass=ModelMeta): | ||
... | ||
|
||
class MetaClassModel(ModelBaseWithMeta): | ||
if not TYPE_CHECKING: | ||
string_: tuple | ||
integer_: tuple | ||
else: | ||
string_: str | ||
integer_: int | ||
|
||
MetaClassModel(string_="abc", integer_=1) | ||
MetaClassModel(string_="abc", integer_=tuple()) # E: Argument "integer_" to "MetaClassModel" has incompatible type "Tuple[<nothing>, ...]"; expected "int" | ||
|
||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/dataclasses.pyi] | ||
|
||
[case testDataclassTransformConditionalAttributes] | ||
# flags: --python-version 3.11 --always-true TRUTH | ||
from typing import dataclass_transform, Type, TYPE_CHECKING | ||
|
||
TRUTH = False # Is set to --always-true | ||
|
||
@dataclass_transform() | ||
def model(cls: Type) -> Type: | ||
return cls | ||
|
||
@model | ||
class FunctionModel: | ||
if TYPE_CHECKING: | ||
present_1: int | ||
else: | ||
skipped_1: int | ||
if True: | ||
present_2: int | ||
if not False: | ||
present_3: int | ||
if not TRUTH: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add at least a single test with some if cond():
x: int
y: int
z1: int
else:
x: str
y: int
z2: int There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added the test. It is of course expecting errors, because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think the test is correct 👍 |
||
skipped_2: int | ||
else: | ||
present_4: int | ||
|
||
FunctionModel( | ||
present_1=1, | ||
present_2=2, | ||
present_3=3, | ||
present_4=4, | ||
) | ||
FunctionModel() # E: Missing positional arguments "present_1", "present_2", "present_3", "present_4" in call to "FunctionModel" | ||
FunctionModel( # E: Unexpected keyword argument "skipped_1" for "FunctionModel" | ||
present_1=1, | ||
present_2=2, | ||
present_3=3, | ||
present_4=4, | ||
skipped_1=5, | ||
) | ||
|
||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/dataclasses.pyi] |
Uh oh!
There was an error while loading. Please reload this page.