facebook / pyre-check

Performant type-checking for python.
https://pyre-check.org/
MIT License
6.85k stars 437 forks source link

Mutable attributes of classes should be invariant in subclasses. #789

Open mikeshardmind opened 1 year ago

mikeshardmind commented 1 year ago

Pyre Bug

Bug description

Subclasses should not be able to change the type of their mutable attributes, see relevant open issue in mypy and recent discussion in python/typing

Reproduction steps

class UStr(str):
    pass

class A:
    foo: str
    def __init__(self):
        self.foo = ""

class B(A):
    foo: UStr
    def __init__(self):
        self.foo = UStr("")

Expected behavior Pyre should not allow the subclass B

Logs

No Errors!

Please run your reproduction steps followed by pyre rage > pyre_rage.log, and upload the file here:

**Additional context** This was run in [the playground](https://pyre-check.org/play?input=class%20UStr(str)%3A%0A%20%20%20%20pass%0A%0A%0Aclass%20A%3A%0A%20%20%20%20foo%3A%20str%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.foo%20%3D%20%22%22%0A%0Aclass%20B(A)%3A%0A%20%20%20%20foo%3A%20UStr%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.foo%20%3D%20UStr(%22%22)%0A) to verify, as I am not a pyre user, only bringing this to the attention of pyre due to ecosystem impact
connernilsen commented 1 year ago

Hey @mikeshardmind, thanks for reporting this to us! I think this is something that we have run into trouble with before, but we didn't have the capacity at the time to implement a fix. I'll go through and see if it's something we're interested in coming back to, but either way, I'll add it to our backlog.