Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

covariant overriding of fields considered unsafe #45

Closed
gvanrossum opened this issue Sep 12, 2017 · 3 comments
Closed

covariant overriding of fields considered unsafe #45

gvanrossum opened this issue Sep 12, 2017 · 3 comments

Comments

@gvanrossum
Copy link

I don't recall where I saw this but I believe we had some example where a base class would declare a field of type float and the subclass would redefine it as int. I would just like to insert a note in that thread referencing python/mypy#3208, which points out that this is not actually a safe thing to do unless the class is immutable.

@ilevkivskyi
Copy link
Contributor

Yes, but mypy currently only rejects this for protocols (which are deliberately more strict in some aspects), so technically speaking the example will pass, but I agree Any (probably in supertype) may be better for covariant mutable overriding example.

@ericvsmith
Copy link
Owner

It was in a test.

        # Note that x from C1 replaces x in Base, but the order remains
        #  the same as defined in Base.
        @dataclass
        class Base:
            x: float = 15.0
            y: int = 0

        @dataclass
        class C1(Base):
            z: int = 10
            x: int = 15

I'm not sure we're using "super" and "sub" the same way here. Should the type of x be Any in base, or in C1?

@ilevkivskyi
Copy link
Contributor

I think it would be better with Any in Base.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants