Skip to content

Commit 1d7a6ae

Browse files
authored
Use variable name to determined NamedTuple class name (#6698)
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 dae2ae8 commit 1d7a6ae

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

test-data/unit/check-incremental.test

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

50625062
[case testNewAnalyzerIncrementalBrokenNamedTupleNested]
50635063

test-data/unit/fine-grained.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9622,3 +9622,26 @@ class C:
96229622
[out]
96239623
==
96249624
main:5: error: Unsupported left operand type for + ("str")
9625+
9626+
[case testReexportNamedTupleChange]
9627+
from m import M
9628+
9629+
def f(x: M) -> None: ...
9630+
9631+
f(M(0))
9632+
9633+
[file m.py]
9634+
from n import M
9635+
9636+
[file n.py]
9637+
from typing import NamedTuple
9638+
M = NamedTuple('_N', [('x', int)])
9639+
9640+
[file n.py.2]
9641+
# change the line numbers
9642+
from typing import NamedTuple
9643+
M = NamedTuple('_N', [('x', int)])
9644+
9645+
[builtins fixtures/tuple.pyi]
9646+
[out]
9647+
==

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