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

invalid error when writing to an abstract property in an abstract class #3011

Closed
funkyfuture opened this issue Mar 15, 2017 · 4 comments
Closed

Comments

@funkyfuture
Copy link

i have this piece of code:

class NamespacedXMLDocument(XMLDocumentBase, metaclass=abc.ABCMeta):

    @abc.abstractproperty
    def class_namespaces(self) -> Tuple[str]:
        pass

    @classmethod
    def register_xml_namespace(cls, namespace: str) -> None:
        cls.class_namespaces = cls.class_namespaces + (namespace,)

and mypy 0.501 yields this for the last line:

Incompatible types in assignment (expression has type Tuple[str, ...], 
variable has type Callable[[NamespacedXMLDocument], Tuple[str]])

my interpretation is, that Callable relates to abc.abstractproperty.

@gvanrossum
Copy link
Member

I think this is also a repro:

import abc
class C(metaclass=abc.ABCMeta):
    @abc.abstractproperty
    def foo(self) -> int: pass
    @classmethod
    def bar(cls) -> None:
        cls.foo = 0

This gives a similar error on the last line:

Incompatible types in assignment (expression has type "int", variable has type Callable[[C], int])

But is it really wrong? You're trying to dynamically patch the definition of the property in the class.

@funkyfuture
Copy link
Author

my intent is to define an abstract property class_namespaces that must be defined in subclasses of the abstract class and to provide a non-abstract method inherited by all subclasses that controls that - then in the subclass' context non-abstract - property for each subclass. does that make sense?

@gvanrossum
Copy link
Member

What you want is a class property. There's no stdlib support for those, but IIRC there are some hints on StackOverflow. I assume that what you tried doesn't work at runtime either.

@funkyfuture
Copy link
Author

indeed i want an abstract class property. it does actually work that way, however it doesn't fail as expected if a subclass doesn't define that class property.

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

2 participants