Skip to content

Mypy improvements#890

Merged
hynek merged 7 commits into
mainfrom
tin/mypy-fields
May 1, 2022
Merged

Mypy improvements#890
hynek merged 7 commits into
mainfrom
tin/mypy-fields

Conversation

@Tinche

@Tinche Tinche commented Dec 17, 2021

Copy link
Copy Markdown
Member

Not to be merged yet.

The problem is the Mypy plugin doesn't return a namedtuple, but an ordinary tuple. We should fix this in the Mypy plugin.

@hynek

hynek commented Feb 2, 2022

Copy link
Copy Markdown
Member

Is this something that should remain open?

@Tinche

Tinche commented Feb 2, 2022

Copy link
Copy Markdown
Member Author

Yeah, we need to merge this when Mypy release 0.940 I believe.

@hynek

hynek commented Feb 2, 2022

Copy link
Copy Markdown
Member

Maybe we should get it to pass CI until then. 😇

@Tinche

Tinche commented Feb 2, 2022

Copy link
Copy Markdown
Member Author

I can't until Mypy releases :p

@hynek

hynek commented Feb 2, 2022

Copy link
Copy Markdown
Member

eg ok, your phrasing sounded like it's needed, once mypy is out

@Tinche

Tinche commented Feb 2, 2022

Copy link
Copy Markdown
Member Author

It's just more sophisticated type stubs, we don't have to match their release exactly. But we might want to.

@hkclark

hkclark commented Feb 2, 2022

Copy link
Copy Markdown
Contributor

@Tinche I noticed recently that using attrs 21.4.0 and the new attrs namespace didn't seem to be supported by mypy. I was initially thinking this PR might add it, but in taking a quick look at the changes, it doesn't seem so (but I'm not expert on mypy type syntax). I was just curious if there was a plan to get support for the attrs namespace or if I was just doing something wrong on my end. :-) Thanks for your work on this!

@hynek

hynek commented Feb 2, 2022

Copy link
Copy Markdown
Member

I noticed recently that using attrs 21.4.0 and the new attrs namespace didn't seem to be supported by mypy.

The new attrs namespace is absolutely supported by mypy. I use it all the time.

We've achieved it by re-importing symbols from the attr namespace and mypy can resolve that.

@hkclark

hkclark commented Feb 2, 2022

Copy link
Copy Markdown
Contributor

I noticed recently that using attrs 21.4.0 and the new attrs namespace didn't seem to be supported by mypy.

The new attrs namespace is absolutely supported by mypy. I use it all the time.

We've achieved it by re-importing symbols from the attr namespace and mypy can resolve that.

Hmm, OK, thanks for the info! I guess let me convert this project at work back to the attrs namespace and see if I can figure out what's going on. :-)

While I have you, I assume it's expected that mypy cannot handle "fancy converters" -- I get error: Unsupported converter, only named functions and types are currently supported if I do things like: a) converter=lambda value: weakref.proxy(value) or converter=[...] or converter=attrs.validtors.pipe(...).

@hkclark

hkclark commented Feb 2, 2022

Copy link
Copy Markdown
Contributor

@hynek To follow up on the issues I mentioned in this project I'm working on, when I use the attrs namespace for everything I get this from mypy for my converters:

error: Module has no attribute "converters"  [attr-defined]

@hynek

hynek commented Feb 3, 2022

Copy link
Copy Markdown
Member

Unfortunately, mypy support for converters is very bare-bones. There's not much we can do about it. Your error must have some other cause. It's explicitly tested here:

from attrs.converters import optional

@hkclark

hkclark commented Feb 3, 2022

Copy link
Copy Markdown
Contributor

Unfortunately, mypy support for converters is very bare-bones. There's not much we can do about it. Your error must have some other cause. It's explicitly tested here:

from attrs.converters import optional

@hynek OK, thank you. I'll study it more and if I can't find a cause on my side, I'll try to open a ticket with very bare-bones example code that recreates it. And maybe I'll open a PR that adds a comment to the docs re converters being limited just so others know about the limitations. Thank you again!

@Tinche

Tinche commented Mar 14, 2022

Copy link
Copy Markdown
Member Author

@hynek this should go in now. I guess I need to rebase though.

@hynek

hynek commented Mar 14, 2022

Copy link
Copy Markdown
Member

@hkclark turns out you were right and our tests broken: #930 + #931

Sorry for that!

@hynek

hynek commented Mar 14, 2022

Copy link
Copy Markdown
Member

@Tinche this is breaking even on 3.6…wanna wait until this stuff is fixed in mypy? :-/

@hkclark

hkclark commented Mar 14, 2022

Copy link
Copy Markdown
Contributor

@hkclark turns out you were right and our tests broken: #930 + #931

Sorry for that!

@hynek No problem at all. Thanks for getting back to me. I'm just glad to know I wasn't going totally crazy. :-) Thank you again for your work on attrs... I really like it and look forward to ongoing new features.

@Tinche

Tinche commented Mar 16, 2022

Copy link
Copy Markdown
Member Author

Mergy mergy now?

@hynek

hynek commented Mar 16, 2022

Copy link
Copy Markdown
Member

Allmost all of CI redy redy?

@hynek

hynek commented Apr 30, 2022

Copy link
Copy Markdown
Member
  • why does test_mypy hate us, personally?
  • it should be something like "AttrsClassInstance" or "AttrsInstance" for all I care?

@hynek hynek merged commit c4ded79 into main May 1, 2022
@hynek hynek deleted the tin/mypy-fields branch May 1, 2022 16:43
hauntsaninja pushed a commit to python/mypy that referenced this pull request Aug 30, 2023
Since python-attrs/attrs#890 (≥ 22.1.0)
`attrs.fields` is typed to accept a protocol.
Since python-attrs/attrs#997 (≥ 22.2.0)
`attrs.has` is a type-guard.

Support both by removing the explicit error reporting and letting it
fall through to the type stub.

Fixes #15980.
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

Successfully merging this pull request may close these issues.

3 participants