Skip to content

mypy can't tell a "private" private attribute defined in a sub-class as being different from a same-named "private" private attribute defined in the base class. #6505

Closed
@asincero

Description

@asincero

Steps to reproduce

mypy thinks the attribute defined on line 10 refers to the attribute defined on line 5:

import typing

class Base:
    def __init__(self) -> None:
        self.__private_attr: typing.Optional[int] = 69
        
class Derived(Base):
    def __init__(self) -> None:
        super().__init__()
        self.__private_attr = ''

Current behavior

mypy reports the following error:

t1.py:10: error: Incompatible types in assignment (expression has type "str", variable has type "Optional[int]")

Expected behavior

mypy shouldn't report anything.

mypy --version output

mypy 0.670

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions