click: Typing changes in 8.1.4 cause failure of mypy check
Click’s simple example https://github.com/pallets/click#a-simple-example doesn’t pass mypy 1.4.1 since 8.1.4. With click 8.1.3 the example did pass. This affects not only the simple example but also existing projects that use click. I ran mypy on one of my projects and everything passed, ran it 30 minutes later and saw failures out of the blue. Click 8.1.4 was published to PyPI between my checks.
To reproduce:
- Save the simple example as click.py
pip install mypy==1.4.1 click==8.1.3mypy click.py
This is the output:
click.py:3: error: Argument 1 has incompatible type "Callable[[Any, Any], Any]"; expected <nothing> [arg-type]
click.py:12: error: <nothing> not callable [misc]
Environment:
- Python version: 3.11
- Click version: 8.1.4
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 85
- Comments: 28 (20 by maintainers)
Commits related to this issue
- Mark Click version 8.1.4 as incompatible (mypy typing errors) See https://github.com/pallets/click/issues/2558 — committed to gertvdijk/purepythonmilter by gertvdijk a year ago
- Temporarily pin to click < 8.1.4 There is a typing issue with click decorators: https://github.com/pallets/click/issues/2558 [ghstack-poisoned] — committed to Instagram/Fixit by amyreese a year ago
- Temporarily pin to click < 8.1.4 There is a typing issue with click decorators: https://github.com/pallets/click/issues/2558 ghstack-source-id: 26c468f16e6e67dc4aed70ee7d65e1696ea1755a Pull Request ... — committed to Instagram/Fixit by amyreese a year ago
- Update on "Temporarily pin to click < 8.1.4" There is a typing issue with click decorators: https://github.com/pallets/click/issues/2558 [ghstack-poisoned] — committed to Instagram/Fixit by amyreese a year ago
- Temporarily pin to click < 8.1.4 There is a typing issue with click decorators: https://github.com/pallets/click/issues/2558 ghstack-source-id: 26c468f16e6e67dc4aed70ee7d65e1696ea1755a Pull Request ... — committed to Instagram/Fixit by amyreese a year ago
- Downgrade click due to typing issue with Mypy https://github.com/pallets/click/issues/2558 — committed to Tatsh/instagram-archiver by Tatsh a year ago
- Check types against older Click that still has correct types https://github.com/pallets/click/issues/2558 — committed to mkdocs/mkdocs by oprypin a year ago
- Fixes click dependency to 8.1.3 Version 8.1.4 type hint changes are not retro-compatible with 8.1.3 See: https://github.com/pallets/click/issues/2558 — committed to Nicals/saulve by Nicals a year ago
- ignore mypy errors due to https://github.com/pallets/click/issues/2558 — committed to neuro-inc/mlops-wandb-bucket-ref by YevheniiSemendiak a year ago
- Bump neuro-cli for release (#140) * bump neuro-cli * bump python version * bump deps * ignore mypy errors due to https://github.com/pallets/click/issues/2558 — committed to neuro-inc/mlops-wandb-bucket-ref by YevheniiSemendiak a year ago
- chore(pre-commit): Pin versions of Python deps Dodges https://github.com/pallets/click/issues/2558 — committed to HPCToolkit/hpctoolkit by blue42u a year ago
- click 8.1.4의 type checking 문제 우회 https://github.com/pallets/click/issues/2558 — committed to item4/yui by item4 a year ago
- Avoid a buggy Click version A spurious typing error in this version is detailed in: https://github.com/pallets/click/issues/2558 — committed to cucapra/turnt by sampsyo a year ago
- CI fix for Click typing issue https://github.com/pallets/click/issues/2558 — committed to hauntsaninja/black by hauntsaninja a year ago
- Fix CI for Click typing issue (#3770) https://github.com/pallets/click/issues/2558 — committed to psf/black by hauntsaninja a year ago
- Exclude click==8.1.4 due to typing problems See pallets/click/issues/2558 — committed to AntiCompositeNumber/iNaturalistReviewer by AntiCompositeNumber a year ago
- Exclude click==8.1.4 due to typing problems See pallets/click/issues/2558 — committed to AntiCompositeNumber/iNaturalistReviewer by AntiCompositeNumber a year ago
- Temporarily pin more dependencies to allow release - Click 8.1.4 typechecking incompatible with mypy configuration, temporarily pinned until https://github.com/pallets/click/issues/2558 resolved - Fa... — committed to dls-controls/scanspec by DiamondJoseph a year ago
- Pin click to prevent mypy failures (#146) Closes #145 until https://github.com/pallets/click/issues/2558 is resolved — committed to dls-controls/tickit by rosesyrett a year ago
- Temporarily pin more dependencies to allow release (#87) - Click 8.1.4 typechecking incompatible with mypy configuration, temporarily pinned until https://github.com/pallets/click/issues/2558 resolve... — committed to dls-controls/scanspec by DiamondJoseph a year ago
Click 8.1.6 is available on PyPI.
I can put together a PR over the weekend. How about I set up some typing tests too (by stealing them from @hynek in attrs)?
Click 8.1.5 is available on PyPI.
Thanks @Tinche and @sirosen for helping diagnose and fix the issue.
That was what I used originally. It was apparently lacking, because we continue to get reports from users saying that their type checker doesn’t like something, then we add more complexity to the type, then something else isn’t correct, etc. Python’s and mypy’s implementation of typing can be a huge mess sometimes.
Whoever is going to change this, be sure to follow
git blame(or GitHub’s view of it) back in time until you get to the original addition of types. Make sure to take into account why the various commits were made along the way.Locking to avoid “me too” comments.
I’m still experiencing this issue, the examples in https://click.palletsprojects.com/en/8.1.x/quickstart/#nesting-commands fail to typecheck under MyPy 1.4.1. I imagine whatever type adjustments were done to
click.commandfor 8.1.5 also needs to be done toclick.group.PR up at https://github.com/pallets/click/pull/2562. I’ve also added simple Mypy and Pyright tests that should be fleshed out over time.
Here’s a very minimal reproduction of this issue using just
version_option(which returns_Decorator[FC]) that might help narrow things down:Taking a quick look at this.
Would you consider changing the signature of
option(and maybe others) to:? This syntax is equivalent to what we have today.
The current implementation with a generic type alias might be a little too much for Mypy at this time (I’m getting
note: Revealed type is "<nothing>"). This by itself won’t solve this issue but it might be a necessary step.After that, I think this overload is the issue:
If I comment it out, the example type-checks. So we need to figure out how to fix it. Given the comment, it might not be super easy.
A minor note: I’m now fairly certain that my “solution” above is incorrect.
If
FC2is defined as an alias for_AnyCallable | Command, then it won’t allow subtypes of_AnyCallable, which is not acceptable. (It would mean that a command whose callback is defined with a return type other thanAnygets rejected.)It might be okay to use
Callable(with no parameters) in lieu of_AnyCallable, but I haven’t yet learned why_AnyCallableis being used rather thanCallable.