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

typing error in combination with pylance for factory=list #810

Closed
matt-the-midnight-hacker opened this issue May 10, 2021 · 5 comments
Closed
Labels
Typing Typing/stub/Mypy/PyRight related bugs.

Comments

@matt-the-midnight-hacker
Copy link

matt-the-midnight-hacker commented May 10, 2021

Hello I'm very excited that attrs now allows to show constructor parameters when used with pylance.
I'm using attrs-21.2.0
However I have a problem when using a class with factory, the factory appears to be not recognized as default value. For example:

@attr.s(auto_attribs=True)
class A:
    my_i: int = 42
    my_list: List[str] = attr.ib(factory=list)

If I try to instantiate this class with a = A() I get error from pylance: 'Argument missing for parameter "my_list"'.
And for the class definition I get the error "Fields without default values cannot appear after fields with default values".
I noticed it works somehow when I use my_list: List[str] = attr.Factory(list) but with this the type is set to Factory and I get problems if I want to use the list a.my_list.append('fu') will then lead to "Instance of 'Factory' has no 'append' member".

@matt-the-midnight-hacker matt-the-midnight-hacker changed the title typing error in combination with pylance typing error in combination with pylance for factory=list May 10, 2021
@hynek
Copy link
Member

hynek commented May 10, 2021

Yes, this has been raised in #795 (comment) . I hope the pyright team sees it our way…

@matt-the-midnight-hacker
Copy link
Author

Oh ok, if there is already a ticket for this issue then feel free to close this one. Many thanks for all the hard work you guys do. 🙇

@euresti
Copy link
Contributor

euresti commented May 10, 2021

Can you work around it with this?

@attr.s(auto_attribs=True)
class A:
    my_i: int = 42
    my_list: List[str] = attr.Factory(list)

Oh ugg:

I noticed it works somehow when I use my_list: List[str] = attr.Factory(list) but with this the type is set to Factory and I get problems if I want to use the list a.my_list.append('fu') will then lead to "Instance of 'Factory' has no 'append' member".

Looks like pylance doesn't do the Factory magic that we do in the stubs.

@matt-the-midnight-hacker
Copy link
Author

matt-the-midnight-hacker commented Jun 28, 2021

Sadly not, maybe pylance could do? Seems like that there is some trouble in finding a aggreement which project will handle default_factory/attr.ib(factory=list) (#795 (comment)). Maybe supporting attr.Factory(list) in pylance is an option? It's somehow "half" supported. On hovering the variable I get the correct type hint (List) but still pyright shows errors that its instance of Factory.

@wsanchez wsanchez added the Typing Typing/stub/Mypy/PyRight related bugs. label Aug 23, 2021
@hynek
Copy link
Member

hynek commented Apr 5, 2022

Great news, they added factory!

@hynek hynek closed this as completed Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing Typing/stub/Mypy/PyRight related bugs.
Projects
None yet
Development

No branches or pull requests

4 participants