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

@property on subclass raises error #8912

Closed
gibiansky opened this issue May 28, 2020 · 6 comments
Closed

@property on subclass raises error #8912

gibiansky opened this issue May 28, 2020 · 6 comments

Comments

@gibiansky
Copy link

Defining a property in a superclass and then using a @property decorator in a subclass does not detect that the signatures match.

I think this is a bug, but I'm not sure if there are some edge cases I haven't thought through that make this error valid.

Here's code:

class Superclass:                    
    prop: int                   
                                     
class Subclass(Superclass):          
    @property                        
    def prop(self) -> int: ...

The mypy error:

test.py:10: error: Signature of "prop" incompatible with supertype "Superclass"

Mypy --version is mypy 0.770.

I tried to look through the list of issues with @property but couldn't determine if this is a duplicate; the others (that seem largely resolved) have to do with @property whereas this has to do with @property when in a subclass with a superclass that has a type annotation.

This might be a duplicate of #5936; if so, please close this. I wasn't sure because that seems to have to do with setters, which this does not.

@JelleZijlstra
Copy link
Member

This is because prop is assignable on the superclass but not the subclass.

Example:

def f(x: Superclass):
    x.prop = 42

f(Subclass())  # fails at runtime, but mypy is OK with it

@gibiansky
Copy link
Author

Ah, thank you. Could there be a clearer error message for this?

Also, should this work then:

class Superclass:                    
    prop: int                        
                                     
                                     
class Subclass(Superclass):          
    @property                        
    def prop(self) -> int:           
        ...                          
                                     
    @prop.setter                     
    def prop(self, val: int) -> None:
        ...                          

Because I get the same errors there, but that addresses this concern.

@JelleZijlstra JelleZijlstra reopened this May 28, 2020
@JelleZijlstra
Copy link
Member

Hm, yes. That should be allowed unless I'm missing something.

Technically it's unsafe because the base class would allow del obj.prop and the subclass wouldn't, but I don't think mypy is that pedantic.

@rik
Copy link

rik commented Jun 5, 2020

I think this is a duplicate of #4125

@JukkaL
Copy link
Collaborator

JukkaL commented Jun 8, 2020

Yeah, looks like a duplicate of #4125.

@JukkaL JukkaL closed this as completed Jun 8, 2020
@gibiansky
Copy link
Author

Thanks for figuring this out!

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

4 participants