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

Attributes of property objects can't be accessed #8085

Open
JukkaL opened this issue Dec 5, 2019 · 4 comments
Open

Attributes of property objects can't be accessed #8085

JukkaL opened this issue Dec 5, 2019 · 4 comments
Labels
false-positive mypy gave an error on correct code feature priority-1-normal

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 5, 2019

Originally reported by @ilevkivskyi in #220 (comment):

Another thing that doesn't work came in #6045: accessing attributes of a property object in the class body (for example fset, fget etc). This can be probably partially solved by some special casing in checkmember.py.

@JukkaL JukkaL added feature priority-1-normal false-positive mypy gave an error on correct code labels Dec 5, 2019
@Pathfinder216
Copy link

If I understand this correctly, I just ran into an issue related to this. I get a mypy error when overriding a property in a subclass.

class Base:
    @property
    def foo(self):
        return self._foo

    @foo.setter
    def foo(self, val):
        self._foo = val


class Sub(Base):
    @Base.foo.setter  # mypy error
    def foo(self, val):
        self._foo = val + 1

The error I get:

foo.py:12: error: overloaded function has no attribute "setter"

@harahu
Copy link

harahu commented Dec 9, 2022

I encountered this issue when trying to use a Result-like wrapper type, and I want to interact with the property of a wrapped object.

The following program runs fine, but generates a mypy error:
error: "Callable[[Person], str]" has no attribute "fget" [attr-defined]

https://gist.github.com/mypy-play/12b11a6d77c3e70413514c4666015aa2

from dataclasses import dataclass

from result import Ok


@dataclass
class Person:
    _name: str

    @property
    def name(self) -> str:
        return self._name.title()


person_result = Ok(Person(_name="john doe"))

name_result = person_result.map(Person.name.fget)

name = name_result.value

print(name)

I could avoid the false positive by using lambda p: p.name, in place of Person.name.fget, but I prefer the latter because it clearly documents what I expect the to reside within the wrapper, which I think helps readability.

P.S: Maybe of interest to @sobolevn given the functional patterns at play? 😉

@eltoder
Copy link

eltoder commented Feb 20, 2023

A minimal example for clarification:

class C:
    @property
    def p(self) -> int:
        return 1

C.p.fget
C.p.fset
C.p.fdel
$ mypy --strict t.py
t.py:6: error: "Callable[[C], int]" has no attribute "fget"  [attr-defined]
t.py:7: error: "Callable[[C], int]" has no attribute "fset"  [attr-defined]
t.py:8: error: "Callable[[C], int]" has no attribute "fdel"  [attr-defined]
Found 3 errors in 1 file (checked 1 source file)

mypy believes that C.p is Callable[[C], int] rather than a property wrapping that function and generates false positives on access.

@josmithua
Copy link

Still an issue a few years later. Can we get this fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive mypy gave an error on correct code feature priority-1-normal
Projects
None yet
Development

No branches or pull requests

5 participants