I wish to type-annotate a function that takes an AnyStr argument that defaults to a str and also returns an AnyStr of the same type. However, if I write this: then mypy fails with “Incompatible default for argument “s” (default has type “str”, argument has type “bytes”)”. I also tried splitting the code into a .py and .pyi file like
Tag: python-typing
TypeError: Cannot instantiate typing.Optional [closed]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers. Closed 2 years ago. Improve this question
Why are type hints for variables not handled as type hints for function parameters?
When writing a function in Python with type hints like this: It translates to this type hint: Optional[Token]. With optional, a None value is accepted too. When writing the same type hint for a class field, it doesn’t behave the same: Here type hint checkers like the integrated one in PyCharm reports: Expected type ‘Token’, got None instead. My questions
Differentiate class and object in Python type hints
Django’s post_save signal sends a model class argument – sender – along with the actual instance being saved – instance. Is there a way to differentiate between the two in type hints? Example We have a model User and would like to create a post_save signal: As you can see, I have given both sender and instance the same type
PEP484 typing — how to annotate Callable with NoReturn type?
Is it possible to annotate Callable with the NoReturn type? The way I would expect to do this yields an error: Edit: for anyone who encounters this problem in the future, the issue was a bug in Python 3.7.0 and upgrading to Python 3.7.2 mitigates the issue. Answer For me (with Python 3.7.2) it works without an error:
Type hints with user defined classes
Couldn’t seem to find a definitive answer. I want to do a type hint for a function and the type being some custom class that I have defined, called it CustomClass(). And then let’s say in some function, call it FuncA(arg), I have one argument named arg. Would the correct way to type hint FuncA be: Or would it be:
Python type hinting without cyclic imports
I’m trying to split my huge class into two; well, basically into the “main” class and a mixin with additional functions, like so: main.py file: mymixin.py file: Now, while this works just fine, the type hint in MyMixin.func2 of course can’t work. I can’t import main.py, because I’d get a cyclic import and without the hint, my editor (PyCharm) can’t
Type hinting / annotation (PEP 484) for numpy.ndarray
Has anyone implemented type hinting for the specific numpy.ndarray class? Right now, I’m using typing.Any, but it would be nice to have something more specific. For instance if the NumPy people added a type alias for their array_like object class. Better yet, implement support at the dtype level, so that other objects would be supported, as well as ufunc. Answer
What are type hints in Python 3.5?
One of the most talked-about features in Python 3.5 is type hints. An example of type hints is mentioned in this article and this one while also mentioning to use type hints responsibly. Can someone explain more about them and when they should be used and when not? Answer I would suggest reading PEP 483 and PEP 484 and watching