static-typing

How to statically get TypeVar parameters from a Generic for use in static type checking?

我与影子孤独终老i 提交于 2021-02-08 10:40:36
问题 I have a class that inherits from typing.Generic and passes in one TypeVar as the parameter. Later on in the code, I would like to: Statically (not at runtime) get the TypeVar parameter out from the class Alias it to another type variable Use that alias to type hint the return of a function Is there some way in Python that I can make this happen? The only piece I am missing is step 1, how to get the type parameter out of a type variable My Use Case from abc import ABC, abstractmethod from

Do type annotations in Python enforce static type checking?

杀马特。学长 韩版系。学妹 提交于 2021-01-29 10:33:07
问题 I'm trying to enforce some static type checking in a project written in Python, using typing module. When I define a function like the one from the doc def greeting(name: str) -> str: return 'Hello ' + name and try to do something like greeting(3) , I indeed got the following error Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in greeting TypeError: must be str, not int But when I again define a function called test def test(a: int) -> None: