Stewori / pytypes

Typing-toolbox for Python 3 _and_ 2.7 w.r.t. PEP 484.
Apache License 2.0
200 stars 20 forks source link

Support Python 3.10 #116

Open llchan opened 2 years ago

llchan commented 2 years ago

Some code needs to be updated now that the deprecated top-level ABCs in collections have been removed.

  File "/path/to/lib/python3.10/site-packages/pytypes/type_util.py", line 2252, in <module>
    class _typechecked_Iterable(collections.Iterable):
AttributeError: module 'collections' has no attribute 'Iterable'
baterflyrity commented 2 years ago

+1, also want to include this library to my project.

Stewori commented 2 years ago

Sorry, I am not able to fix this in foreseeable future. There is more to it than the collections issue. Already even Python 3.9 compatibility would require some serious work. When I started this library I did not expect it to require half a rewrite for every new Python release. However, if someone has a fix or improvement to offer (as a PR), be my guest.

sveinugu commented 1 year ago

@Stewori:

I am trying to follow the history (in typing and elsewhere) of the once suggested replacements of issubclass and isinstance for types, partly implemented in https://github.com/python/typing/pull/207, which was replaced by https://github.com/python/typing/pull/283, but without such methods due to a disappointing decision to leave this up to third-party libs. In this typing_inspect issue, a torch was passed on to pytypes, which seems to have been a too heavy torch to carry due to all the changes done in the last Python versions (don't get me wrong, I am impressed that you carried it this far!).

From the outside, it seems to me that the decision to leave the type-equivalents of issubclass and isinstance out of typing was a mistake. The functionality seems to belong there, and if the reason for leaving it out was due to the complexity of implementing the functionality, then that fact should rather have worked as a feedback loop to help them simplify the typing implementation, IMO.

That discussion aside, my current trail ends here, and I want to ask you whether you can point me to other libraries that implement this functionality and which are also updated to Python 3.10, if any. Do you know if there are any third-party libraries that are considered more reference implementations than others?

baterflyrity commented 1 year ago

@sveinugu , what about mypy? Most static type checkers use mypy hence it can be used in dynamic type checks too.

sveinugu commented 1 year ago

@sveinugu , what about mypy? Most static type checkers use mypy hence it can be used in dynamic type checks too.

Thanks for the feedback. I was under the impression that mypy was only for static type checking, but I suppose you are right. However, I tried now and I am not easily able to make this work:

>>> from typing import Any
>>> from mypy.subtypes import is_subtype
>>> is_subtype(list[str], list[Any])
Traceback (most recent call last):
  File "/Users/sveinugu/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/223.8617.48/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 1, in <module>
  File "mypy/subtypes.py", line 114, in is_subtype
TypeError: mypy.types.Type object expected; got types.GenericAlias

As this is meant for a production library I would rather make use of an API that is meant to be used dynamically. In any case, I presume I may be missing something?

baterflyrity commented 1 year ago

@sveinugu , well, mypy can not directly check types in runtime (states by one of maintainers). Nevertheless, there are several tricks:

  1. You can create a temporary file and call mypy like for static check.
  2. You can check argument as code.

For both tricks I found mypy.api.run method suitable. Check out my example:

>>> from mypy.api import run
>>> run(['-c', 'x:list[int]=1,2'])
('<string>:1: error: Incompatible types in assignment (expression has type "Tuple[int, int]", variable has type "List[int]")\nFound 1 error in 1 file (checked 1 source file)\n', '', 1)
>>> run(['-c', 'x:list[int]=[1,2]'])
('Success: no issues found in 1 source file\n', '', 0)
sveinugu commented 1 year ago

@sveinugu , well, mypy can not directly check types in runtime (states by one of maintainers). Nevertheless, there are several tricks:

  1. You can create a temporary file and call mypy like for static check.
  2. You can check argument as code.

For both tricks I found mypy.api.run method suitable. Check out my example:

>>> from mypy.api import run
>>> run(['-c', 'x:list[int]=1,2'])
('<string>:1: error: Incompatible types in assignment (expression has type "Tuple[int, int]", variable has type "List[int]")\nFound 1 error in 1 file (checked 1 source file)\n', '', 1)
>>> run(['-c', 'x:list[int]=[1,2]'])
('Success: no issues found in 1 source file\n', '', 0)

Thanks!

I am not very happy to use a hack like this, but if that is what’s possible, then at least it should be working. I might be able to solve my use case in another matter though.. in any case, thanks for the help!