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

Type fixes #232

Merged
merged 2 commits into from
Jul 21, 2024
Merged

Type fixes #232

merged 2 commits into from
Jul 21, 2024

Conversation

DeviousStoat
Copy link
Contributor

fixes #231

@DeviousStoat
Copy link
Contributor Author

I have no idea why we get these mypy syntax errors though. Doesn't even seem to point to the right line and I can't seem to be able to reproduce locally. I will try to figure it out later today

@dgilland
Copy link
Owner

I can reproduce it locally if I delete the .mypy_cache folder and then run mypy (but only on the first run):

$ rm -r .mypy_cache
$ inv mypy
src/pydash/numerical.py:104:68: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/pydash/arrays.py:104:68: error: X | Y syntax for unions requires Python 3.10  [syntax]
src/pydash/objects.py:104:68: error: X | Y syntax for unions requires Python 3.10  [syntax]
$ inv mypy
$

I don't think there's even any X | Y type syntax being used either, which makes me think it might be an issue with mypy.

@DeviousStoat
Copy link
Contributor Author

I think I found the issue. It happens when using the SupportsRichComparisonT type variable.

It is defined in the python base library typesheds. When we are using a >=3.10 python version mypy will use the >=3.10 typesheds and the definition is

SupportsRichComparison: TypeAlias = SupportsDunderLT[Any] | SupportsDunderGT[Any]
SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison)

And in the pyproject.toml mypy section we have python_version = "3.8" so it fails when type checking using the 3.10 typeshed with the X | Y union syntax that is used in the typeshed. If we set python_version = "3.10" it works. But I don't think we want that, we probably want mypy to check using the lowest supported python version.

One other thing that fixes it is adding from __future__ import annotations on top of the affected modules. This allows using the X | Y syntax in <3.10 python version. It also postpones the evaluation of annotations so we could write collection: t.Mapping[t.Any, SupportsRichComparisonT], default: Unset = UNSET instead of collection: t.Mapping[t.Any, "SupportsRichComparisonT"], default: Unset = UNSET for type variable that are imported only during TYPE_CHECKING.

Otherwise we could redefine the SupportsRichComparison values inside the library.

I think the from __future__ import annotations is probably the best solution, it's a good practice to support different python versions with mypy. Let me know what you think.

@dgilland
Copy link
Owner

From future import seems best.

@DeviousStoat
Copy link
Contributor Author

neat, everything passes now :D

@dgilland dgilland merged commit e6b2ffa into dgilland:develop Jul 21, 2024
12 checks passed
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.

mypy overload-overlap errors starting with mypy==1.11.0
2 participants