Skip to content

Commit 1fa74ff

Browse files
authored
Fix redefinition logic for InitVar (#22352)
I know this is somewhat pedantic, but I cannot sleep well knowing we have this in our code. Few things here: * Everywhere else in mypy first definition wins, not the last one, so we should do the same in presence of `InitVar`. * Sorting strings is incorrect anyway, as it will put `foo-redefinition-10` before `foo-redefinition-9`. * Limit this special-case only to classes explicitly touched by plugins (as indicated by non-trivial metadata)
1 parent ccbf32b commit 1fa74ff

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

‎mypy/nodes.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4059,11 +4059,11 @@ def get_method(self, name: str) -> FuncBase | Decorator | None:
40594059
for cls in self.mro:
40604060
sym = cls.names.get(name)
40614061
if sym is None:
4062-
# add_redefinition installs the unnumbered name before any numbered ones.
4063-
prefix = f"{name}-redefinition"
4064-
if prefix not in cls.names:
4062+
# Plugins are allowed to meddle with redefinitions (first one wins).
4063+
if cls.metadata:
4064+
sym = cls.names.get(f"{name}-redefinition")
4065+
if sym is None:
40654066
continue
4066-
sym = cls.names[max(n for n in cls.names if n.startswith(prefix))]
40674067
node = sym.node
40684068
if isinstance(node, SYMBOL_FUNCBASE_TYPES):
40694069
return node

0 commit comments

Comments
 (0)