Skip to content

signatures of setters are not checked #12892

Closed

Activity

  1. DetachHead commented on May 28, 2022

    @DetachHead
    ContributorAuthor

    also no error when there's no second argument:

    class A:
        @property
        def foo(self) -> int:
            ...
        @foo.setter
        def foo(self) -> None: # no mypy error
            ...
    
    
    a = A()
    
    a.foo = 1 # runtime error - TypeError: A.foo() takes 1 positional argument but 2 were given
  2. changed the title [-]no error when setter has too many arguments[/-] [+]signatures of setters are not checked[/+] on May 28, 2022
  3. DetachHead commented on Jul 20, 2022

    @DetachHead
    ContributorAuthor

    and also no error when the setter type is different to the getter type

    class Foo:
        @property
        def foo(self) -> int:
            ...
    
        @foo.setter
        def foo(self, value: str) -> None:
            ...

    however you get an error when attempting to use the setter:

    f = Foo()
    f.foo = "" # Incompatible types in assignment (expression has type "str", variable has type "int")
  4. q-wertz commented on Feb 3, 2023

    @q-wertz

    Similar example:

    class A:
        def init(self) -> None:
            self._a: int | None = None
    
        @property
        def a(self) -> int:
            if self._a is None:
                raise ValueError
            return self._a
    
        @a.setter
        def a(self, val: int | None) -> None:
            self._a = val
    
    
    a = A()
    
    a.a = None  # Incompatible types in assignment (expression has type "None", variable has type "int")  [assignment]

    In my scenario I would like it to work like this but if it is bad practice that the setter allows different types than the getter it should warn in the definition of the setter…

  5. ydirson commented on May 12, 2023

    @ydirson

    I would argue that it is not necessarily a problem in itself. We can use this to implement a "smart setter" that takes as type to build on this example Union[int, str], and tries to convert a str to an int.

    What I see as a problem is rather that when you then try to set foo in a way compatible with the declaration and implementation (ie. assign a string to it), then despite the code being correct, mypy will ignore the type declared in the setter, and validate against the getter type instead - see #3004 where the consensus is that this is a valid use case.

    Maybe close this ticket?

  6. DetachHead commented on May 12, 2023

    @DetachHead
    ContributorAuthor

    Even if #3004 gets implemented, there are still several examples here of invalid setters that should be errors (incorrect number of arguments) so I'm leaving this issue open regardless

  7. DetachHead commented on Mar 26, 2025

    @DetachHead
    ContributorAuthor

    none of these examples seem to be fixed as of 1.15.0

  8. JukkaL commented on Mar 27, 2025

    @JukkaL
    Collaborator

    The fix will be in the next feature release. Have you tried master?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-descriptorsProperties, class vs. instance attributestopic-runtime-semanticsmypy doesn't model runtime semantics correctly

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions