Skip to content

Commit ffb9935

Browse files
committed
Skip processing the node, avoid modifying the SymbolTable
1 parent 766bd38 commit ffb9935

File tree

2 files changed

+9
-24
lines changed

2 files changed

+9
-24
lines changed

mypy/plugins/dataclasses.py

+6-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Plugin that provides support for dataclasses."""
22

3-
from typing import Dict, List, Set, Tuple, Optional, Union
3+
from typing import Dict, List, Set, Tuple, Optional
44
from typing_extensions import Final
55

66
from mypy.nodes import (
@@ -16,7 +16,7 @@
1616
from mypy.typeops import map_type_from_supertype
1717
from mypy.types import (
1818
Type, Instance, NoneType, TypeVarType, CallableType, TupleType, LiteralType,
19-
get_proper_type, AnyType, TypeOfAny, TypeType,
19+
get_proper_type, AnyType, TypeOfAny,
2020
)
2121
from mypy.server.trigger import make_wildcard_trigger
2222
from mypy.state import state
@@ -342,23 +342,10 @@ def collect_attributes(self) -> Optional[List[DataclassAttribute]]:
342342
),
343343
node
344344
)
345-
# Now do our best to simulate the runtime,
346-
# which treates a TypeAlias definition in a dataclass class
347-
# as an instance field with a default value.
348-
#
349-
# Replace the `TypeAlias` node with a `Var` node, so that we do the same.
350-
target, fullname = node.target, node.fullname
351-
proper_target = get_proper_type(target)
352-
var_type: Union[TypeType, AnyType]
353-
if isinstance(proper_target, Instance):
354-
var_type = TypeType(proper_target, line=node.line, column=node.column)
355-
# Something else -- fallback to Any
356-
else:
357-
var_type = AnyType(TypeOfAny.from_error)
358-
var = Var(name=node.name, type=var_type)
359-
var.info = cls.info
360-
var._fullname = fullname
361-
sym.node = node = var
345+
# Skip processing this node. This doesn't match the runtime behaviour,
346+
# but the only alternative would be to modify the SymbolTable,
347+
# and it's a little hairy to do that in a plugin.
348+
continue
362349

363350
assert isinstance(node, Var)
364351

test-data/unit/check-dataclasses.test

+3-5
Original file line numberDiff line numberDiff line change
@@ -528,19 +528,17 @@ class One:
528528
S: TypeAlias = Foo # E: Type aliases inside dataclass definitions are not supported at runtime
529529

530530
a = One()
531-
b = One(Foo)
532-
reveal_type(a.S) # N: Revealed type is "Type[__main__.Foo]"
533-
reveal_type(b.S) # N: Revealed type is "Type[__main__.Foo]"
531+
reveal_type(a.S) # N: Revealed type is "def (x: builtins.int) -> __main__.Foo"
534532
a.S() # E: Missing positional argument "x" in call to "Foo"
535533
reveal_type(a.S(5)) # N: Revealed type is "__main__.Foo"
536-
reveal_type(b.S(98)) # N: Revealed type is "__main__.Foo"
537534

538535
@dataclass
539536
class Two:
540537
S: TypeAlias = Callable[[int], str] # E: Type aliases inside dataclass definitions are not supported at runtime
541538

542539
c = Two()
543-
reveal_type(c.S) # N: Revealed type is "Any"
540+
x = c.S # E: Member "S" is not assignable
541+
reveal_type(x) # N: Revealed type is "Any"
544542
[builtins fixtures/dataclasses.pyi]
545543

546544
[case testDataclassOrdering]

0 commit comments

Comments
 (0)