Skip to content

Private / mangled names in subclasses should not conflict with superclasses #2635

@refi64

Description

@refi64

Summary

https://play.ty.dev/bf512978-91c0-434c-ab88-1626323b1de8

class X:
    def __init__(self) -> None:
        self.__value = 0
        print('X.value:', self.__get_value())

    def __get_value(self) -> int:
        return self.__value

class Y(X):
    def __init__(self) -> None:
        super().__init__()
        self.__value = 's'
        print('Y.value:', self.__get_value())
    
    def __get_value(self) -> str:  # ERROR: Definition is incompatible with `X.__get_value`
        return self.__value  # ERROR: expected `str`, found `Unknown | Literal["s", 0]`

Y()

This isn't actually an error, because the name mangling ensures that both value instances are independent:

X.value: 0
Y.value: s

This works in pyright and mypy, partially (the attribute itself still shows up as an error, seems to be python/mypy#8267).

Note that #645 does exist, but I don't really consider that to be a dupe, since it's primarily about proper name mangling (which is needed for the mentioned external access), which is a bit independent from this, e.g. pyright does not allow external access via the mangled names.

Version

a89bcfa0a

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions