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 with different type (getter type != setter type) #4970

Closed
elarivie opened this issue Apr 25, 2018 · 2 comments
Closed

Property with different type (getter type != setter type) #4970

elarivie opened this issue Apr 25, 2018 · 2 comments
Labels
bug mypy got something wrong priority-1-normal

Comments

@elarivie
Copy link

With Mypy 0.590 (This was also the case with previous version), using the flag --strict-optional

Would it be possible to handle a different type for the getter/setter of properties.

In below code example I would expect no Mypy errors, but there are errors each time the property message is assigned a None value, even though the setter arguments list mention that message may be provided a None.

from typing import Optional


class Hello(object):
    @property
    def message(self) -> str:
        """Get the message (Never None)."""
        return self.__message

    @message.setter
    def message(self, value: Optional[str]) -> None:
        """Set the message (Set to empty string if None is provided)."""
        self.__message = "" if value is None else str(value)

    def __init__(self) -> None:
        # Initialize default values of the properties:
        self.message = None   # error: Incompatible types in assignment (expression has type "None", variable has type "str")


x = Hello()
x.message = "World !"
a = "Hello " + x.message
x.message = None  # error: Incompatible types in assignment (expression has type "None", variable has type "str")
@PPACI
Copy link

PPACI commented May 7, 2018

After a quick test, it also affect mypy 0.600 without flag (since --strict-optional is now enabled by default)

@emmatyping emmatyping added bug mypy got something wrong priority-1-normal labels May 7, 2018
@emmatyping
Copy link
Collaborator

Actually this is a duplicate of #3004. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal
Projects
None yet
Development

No branches or pull requests

3 participants