Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2188,7 +2188,14 @@ def check_method_override_for_base_with_name(
else:
override_class_or_static = defn.func.is_class or defn.func.is_static
typ, _ = self.node_type_from_base(defn.name, defn.info, defn)
assert typ is not None
if typ is None:
# This may only happen if we're checking `x-redefinition` member
# and `x` itself is for some reason gone. Normally the node should
# be reachable from the containing class by its name.
# The redefinition is never removed, use this as a sanity check to verify
# the reasoning above.
assert f"{defn.name}-redefinition" in defn.info.names
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sterliakov What about multiple redefinitions? Will this fix still work? The current logic in semanal.py is

            if i == 1:
                new_name = f"{name}-redefinition"
            else:
                new_name = f"{name}-redefinition{i}"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first redefinition cannot be InitVar, so should not get deleted. I can replace this with any(name.startswith(f"{defn.name}-redifinition") for name in info.names), but I couldn't invent the case where name-redefinition is also gone

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, if you can't trigger it, it is fine.

return False

original_node = base_attr.node
# `original_type` can be partial if (e.g.) it is originally an
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -2666,3 +2666,19 @@ class PersonBad(TypedDict):
class JobBad:
person: PersonBad = field(default_factory=PersonBad) # E: Argument "default_factory" to "field" has incompatible type "type[PersonBad]"; expected "Callable[[], PersonBad]"
[builtins fixtures/dict.pyi]

[case testDataclassInitVarRedefinitionNoCrash]
# https://github.com/python/mypy/issues/19443
from dataclasses import InitVar, dataclass

class ClassA:
def value(self) -> int:
return 0

@dataclass
class ClassB(ClassA):
value: InitVar[int]

def value(self) -> int: # E: Name "value" already defined on line 10
return 0
[builtins fixtures/dict.pyi]