-
Notifications
You must be signed in to change notification settings - Fork 6
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
mypy
typing fixes
#5
Conversation
As per the line 5 lower, `case UnresolvedSymbol(None)` is a possible value for `name`, so it should be annotated as optional
Hi @alexpovel , thanks for your interest. Mypy does not support the match statement (and this was one of the new features I wanted to experiment with): python/mypy#10201 I probably will replace the matching until this is fixed (and replace the decorator package) as I would love to try out mypyc to compile to native code. I received some questions regarding performance and that would certainly be the quickest and coolest way to improve |
design-by-contract/poetry.lock Line 491 in 4e99e28
It worked in case of my project because that's using version So it's an easy and obvious one. I had ran $ mypy .\src\design_by_contract.py
src\design_by_contract.py:31: error: Return type "UnresolvedSymbol" of "__eq__" incompatible with return type "bool" in supertype "object"
src\design_by_contract.py:44: error: Incompatible return value type (got "bool", expected "UnresolvedSymbol")
src\design_by_contract.py:62: error: The first argument to Callable must be a list of types, parameter specification, or "..."
src\design_by_contract.py:62: note: See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas
src\design_by_contract.py:67: error: Variable "design_by_contract.R" is not valid as a type
src\design_by_contract.py:67: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Found 4 errors in 1 file (checked 1 source file) |
Ran `poetry add --dev mypy@latest`
`error: Return type UnresolvedSymbol of __eq__ incompatible with return type bool in supertype object [override]`: correct, but this is overridden on purpose
Apparently, this was too much for mypy to handle statically, it couldn't figure out that `P` is a `ParamSpec` etc: ``` src\design_by_contract.py:62: error: The first argument to Callable must be a list of types, parameter specification, or "..." [misc] src\design_by_contract.py:62: note: See https://mypy.readthedocs.io/en/stable/kinds_of_types.html#callable-types-and-lambdas src\design_by_contract.py:67: error: Variable "design_by_contract.R" is not valid as a type [valid-type] src\design_by_contract.py:67: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases src\design_by_contract.py:82: error: Returning Any from function declared to return R? [no-any-return] ``` All these are fixed by undoing the tuple assignment and doing it 'manually'.
We could do `np.ndarray[Any, np.dtype[A]]`, for example, but `NDArray` is a shorthand: https://numpy.org/doc/stable/reference/typing.html#numpy.typing.NDArray Using `Any` is improper, a `TypeVar` would be better to signal all these `Any` are actually the same type, but couldn't get that working.
Having
The first one on line 44 is difficult, I'm not sure about your intent. It's returning As for the latter two: |
Great. I should have read the release notes. Replacing the decorators package is trivial (initially, it reduced more complexity than in the final version). Will give mypyc a shot too on the weekend |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing! together with a modified return type for the __eq__
function, this removes the mypy errors (except for the decorator package which I will remove after merging). Big Thanks! My only ask is to revert the logging to the former style for performance reasons
Also gets rid of having to ignore the type, so it's cleaner
Okay, figured out the decorators (disadvantage: parantheses mandatory after |
Assuming this is how imports are sorted!
Thanks very much! I learned a bit here. |
Great! Glad this was useful to you. Just wanted to mention that |
Python tooling came a long way indeed. It really was enjoyable to bootstrap a complete project (albeit tiny) to experiment with all the new features. And I am pleasantly surprised by receiving support :-) |
Two short fixes.
Please note I couldn't check the validity of 27267da :
mypy
wouldn't run. It's utterly baffling, especially since just the other day I happily ranmypy
in apoetry
project in a venv on code usingmatch
statements. However, for your code:How is that possible? The
mypy
script points to the virtual environment. Python is on 3.10.5. This happens on Windows 10, WSL (Debian), but also in this reproducible Docker environment (initial shell is PowerShell, replace${PWD}
if needed):Can you reproduce this? It's baffling because I see no reason it shouldn't work. This:
design-by-contract/pyproject.toml
Lines 39 to 43 in 4e99e28
looks fine and inconspicuous, commenting that out didn't help any.