Skip to content

Commit 329f91e

Browse files
committed
Use variable name to determined NamedTuple class name
This lets us avoid inserting line numbers into the name when the variable name and argument to NamedTuple disagree. This is good, because the line numbers are ugly and when combined with bug #6548 causes problems when a namedtuple is reexported.
1 parent 974a58d commit 329f91e

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

mypy/semanal_namedtuple.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,20 @@ def check_namedtuple(self,
178178
info = self.build_namedtuple_typeinfo(name, [], [], {}, node.line)
179179
self.store_namedtuple_info(info, name, call, is_typed)
180180
return True, info
181-
name = cast(Union[StrExpr, BytesExpr, UnicodeExpr], call.args[0]).value
182-
if name != var_name or is_func_scope:
183-
# There are three special cases where need to give it a unique name derived
181+
182+
# We use the variable name as the class name if it exists. If
183+
# it doesn't, we use the name passed as an argument. We prefer
184+
# the variable name because it should be unique inside a
185+
# module, and so we don't need to disambiguate it with a line
186+
# number.
187+
if var_name:
188+
name = var_name
189+
else:
190+
name = cast(Union[StrExpr, BytesExpr, UnicodeExpr], call.args[0]).value
191+
192+
if var_name is None or is_func_scope:
193+
# There are two special cases where need to give it a unique name derived
184194
# from the line number:
185-
# * There is a name mismatch with l.h.s., therefore we need to disambiguate
186-
# situations like:
187-
# A = NamedTuple('Same', [('x', int)])
188-
# B = NamedTuple('Same', [('y', str)])
189195
# * This is a base class expression, since it often matches the class name:
190196
# class NT(NamedTuple('NT', [...])):
191197
# ...

test-data/unit/check-incremental.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5036,7 +5036,7 @@ NT = NamedTuple('BadName', [('x', int)])
50365036
[builtins fixtures/tuple.pyi]
50375037
[out]
50385038
[out2]
5039-
tmp/a.py:3: note: Revealed type is 'Tuple[builtins.int, fallback=b.BadName@2]'
5039+
tmp/a.py:3: note: Revealed type is 'Tuple[builtins.int, fallback=b.NT]'
50405040

50415041
[case testNewAnalyzerIncrementalBrokenNamedTupleNested]
50425042

test-data/unit/fine-grained.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9410,3 +9410,26 @@ x: List[C] = [a.f(), a.f()]
94109410
==
94119411
a.py:2: error: "C" expects 2 type arguments, but 1 given
94129412
[builtins fixtures/list.pyi]
9413+
9414+
[case testReexportNamedTupleChange]
9415+
from m import M
9416+
9417+
def f(x: M) -> None: ...
9418+
9419+
f(M(0))
9420+
9421+
[file m.py]
9422+
from n import M
9423+
9424+
[file n.py]
9425+
from typing import NamedTuple
9426+
M = NamedTuple('_N', [('x', int)])
9427+
9428+
[file n.py.2]
9429+
# change the line numbers
9430+
from typing import NamedTuple
9431+
M = NamedTuple('_N', [('x', int)])
9432+
9433+
[builtins fixtures/tuple.pyi]
9434+
[out]
9435+
==

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy