Skip to content

Commit c1ccdd1

Browse files
committed
Remove handling for NamedTuples with non-identifier fields, more tests
Unlike TypedDicts, the functional syntax for NamedTuples does not allow fields that are not valid identifiers, so this check is superfluous.
1 parent 65758a3 commit c1ccdd1

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

mypy/stubtest.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,6 @@ def _is_decoratable(stub: nodes.SymbolNode) -> bool:
464464
name.isidentifier() and not keyword.iskeyword(name)
465465
for name in stub.typeddict_type.items.keys()
466466
)
467-
if stub.is_named_tuple:
468-
return all(
469-
name.isidentifier() and not keyword.iskeyword(name)
470-
for name in stub.metadata["namedtuple"]["fields"]
471-
)
472467
return True
473468

474469

mypy/test/teststubtest.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,6 +2822,53 @@ class _P2: ...
28222822
runtime="class _P2: ...",
28232823
error="_P2",
28242824
)
2825+
# Private TypedDicts which do not exist at runtime must be decorated with
2826+
# '@type_check_only', unless they contain keys that are not valid identifiers.
2827+
yield Case(
2828+
stub="""
2829+
class _TD1(TypedDict): ...
2830+
""",
2831+
runtime="",
2832+
error="_TD1",
2833+
)
2834+
yield Case(
2835+
stub="""
2836+
_TD2 = TypedDict("_TD2", {"foo": int})
2837+
""",
2838+
runtime="",
2839+
error="_TD2",
2840+
)
2841+
yield Case(
2842+
stub="""
2843+
_TD3 = TypedDict("_TD3", {"foo-bar": int})
2844+
""",
2845+
runtime="",
2846+
error=None,
2847+
)
2848+
yield Case(
2849+
stub="""
2850+
_TD4 = TypedDict("_TD4", {"class": int})
2851+
""",
2852+
runtime="",
2853+
error=None,
2854+
)
2855+
# Private NamedTuples which do not exist at runtime must be decorated with
2856+
# '@type_check_only'.
2857+
yield Case(
2858+
stub="""
2859+
class _NT1(NamedTuple): ...
2860+
""",
2861+
runtime="",
2862+
error="_NT1",
2863+
)
2864+
yield Case(
2865+
stub="""
2866+
@type_check_only
2867+
class _NT2(NamedTuple): ...
2868+
""",
2869+
runtime="",
2870+
error=None,
2871+
)
28252872
# A type that exists at runtime is allowed to alias a type marked
28262873
# as '@type_check_only' in the stubs.
28272874
yield Case(

0 commit comments

Comments
 (0)