Skip to content
Advertisement

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

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

Advertisement