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
Optional type hinting stub #260
Comments
I'd prefer only adding annotations when they are easy to understand and not vulnerable to discouraging duck-typing. |
What do you mean @CrazyPython in your last sentence? That we should define 2016-10-16 19:22 GMT+02:00 CrazyPython notifications@github.com:
|
@lrq3000 Yes. |
Ok yes I think that's a good idea, I hope it won't be too hard for us to 2016-10-17 0:21 GMT+02:00 CrazyPython notifications@github.com:
|
Did this go anywhere or is there a stub file available somewhere I can use instead of writing my own? |
don't think it went anywhere :/ |
For python 3.7 the following (tqdm.pyi) was sufficient for my purposes:
it would be nice to have an official file though. |
I suggest to change the useful type stub provided by JamesReynolds to following: from typing import Any, Iterable, Iterator, TypeVar
T = TypeVar("T")
def tqdm(iterable: Iterable[T], *args: Any, **kwargs: Any) -> Iterator[T]: ... This way it can accept wider input. |
from microsoft/pylance-release#233 (comment) slight tweak: from typing import Any, Iterable, Iterator, TypeVar
_T = TypeVar("_T")
def tqdm(iterable: Iterable[_T], *args: Any, **kwargs: Any) -> Iterator[_T]: ... Is this really a valid stub though? The actual function is |
If the real code is a class, it should be typed as a class with a generic. I didn't realize it was a class. e.g.: from typing import Any, Iterable, Iterator, TypeVar, Generic
_T = TypeVar("_T")
class tqdm(Iterator[_T], Generic[_T]):
def __init__(iterable: Iterable[_T], *args: Any, **kwargs: Any) -> None: ... |
atm it's actually Also presumably missing |
This all depends on what is considered the API; are people expected to be able to import Yes, I forgot |
Trivial update I made to the above, if anybody else is using from typing import Any, Iterable, Iterator, TypeVar
_T = TypeVar("_T")
def tqdm(iterable: Iterable[_T], *args: Any, **kwargs: Any) -> Iterator[_T]: ...
def trange(*args: Any, **kwargs: Any) -> Iterator[int]: ... |
Is there any particular reason that this has been hanging ? Tqdm is such a widely used library, it's really a pity that it breaks typing anywhere it is being used. If you want to provide fuller support in the future, maybe in the meantime just supporting the basic I see there was a commit referencing this issue, but the current version on pypi still doesn't seem to have stubs. |
@ldorigo: tqdm does not "break typing anywhere it is being used". Just add I wish the Python community stopped behaving as if static typing is a silver bullet and life cannot go on without them, despite the amazing success that Python has achieved without type hints for more than 25 years. Also, library authors who want to preserve the Pythonic APIs they provide should make extensive use of |
@ramalho I agree 100%. Just going to add that, coming from C/C++ and starting to learn more advanced Python, typing has helped me to understand the language more deeply. |
Not really sure what your point is. If you don't care about typing, good for you. In the meantime, for everyone that does, tqdm does break typing (by hiding type information) and implementing your "fix" requires changing not only their own calls to tqdm, but also those of any library they are using. |
(and for the record, I absolutely agree about duck typing, but it has nothing to do with the issue at hand. Also type annotations are not static typing since they are optional and you're perfectly free to ignore them entirely) |
when type hints? |
Can we move forward with typehints? Do we still need to use the |
@casperdcl Is there a reason you did not open an MR with your typehints? |
have type hints been integrated yet? |
Type hints not only reduce errors but they can greatly improve the editor experience when inference is hard; several of the people commenting on this thread are doing so because they want better auto-completions for tqdm in their editors. |
How about this one? |
Would really appreciate this, right now Pylance isn't able to infer even simple cases like: ls: List[str] = ["a", "b", "c"]
for s in tqdm(ls):
# type of s is not inferred properly statically and is reported as Any
pass Have to rely on manual hinting as such: ls: List[str] = ["a", "b", "c"]
s: str
for s in tqdm(ls):
pass Might be relevant: I'm stuck with using py3.6 (Yeah, I don't like that either) |
That's very cool, but for some reason it doesn't work on my VSCode even though I installed it with pip. |
Yesterday tqdm stubs were added to typeshed, meaning annotations should now work anywhere that uses typeshed for third party stubs (like mypy). |
I was disappointed to learn that this doesn't fix typing in PyCharm. |
Not sure if my problem is related, but I keep encountering typing warning in PyCharm while using from tqdm.auto import tqdm
for i in tqdm(a_list): # Warning: Expected type 'collections.lterable', got 'tqdm_asyncio' instead
... |
This issue has been opened 7 year ago. Any maintainer want to fix it? |
Currently tqdm stubs are managed in typeshed, so we may be able to import them back into this repository once they are grown to some extent. |
also not sure if it relates, but this is literally the only google hit I have on this warning. |
It is related in the sense that this issue needed to be fixed, but it was already fixed (see this comment ) |
thats to bad |
Implement a
.pyi
stub file to implement optional type hinting (usable with mypy or Python >= 3.5) as initially proposed by @CrazyPython.See #198 (comment).
TODO:
Note that the official implementation of type hints in Python 3.5 may change in Python 3.6/3.7, particularly for duck typing support, but since it's an optional stub file (no modification to .py files) we won't lose anything.
The text was updated successfully, but these errors were encountered: