mypy

A way to subclass NamedTuple for purposes of typechecking

霸气de小男生 提交于 2019-11-27 03:55:23
问题 I have several namedtuples that share some fields. I have a function that accepts these tuples and is guaranteed to only interact with the shared fields. I want to typecheck such code in mypy. An example of the code would be: from typing import NamedTuple class Base(NamedTuple): x: int y: int class BaseExtended(NamedTuple): x: int y: int z: str def DoSomething(tuple: Base): return tuple.x + tuple.y base = Base(3, 4) base_extended = BaseExtended(5, 6, 'foo') DoSomething(base) DoSomething(base

How to use type hints in python 3.6?

本秂侑毒 提交于 2019-11-26 22:22:49
I noticed python 3.5 and python 3.6 added a lot of features about static type checking, so I tried with the following code(in python 3.6, stable version). from typing import List a: List[str] = [] a.append('a') a.append(1) print(a) What surprised me was that, python didn't give me an error or warning, although 1 was appended to a list which should only contain strings. Pycharm detected the type error and gave me a warning about it, but it was not obvious and it was not shown in the output console, I was afraid sometimes I might miss it. I would like the following effects: If it's obvious that

How can I specify the function type in my type hints?

柔情痞子 提交于 2019-11-26 20:28:14
I want to use type hints in my current Python 3.5 project. My function should receive a function as parameter. How can I specify the type function in my type hints? import typing def my_function(name:typing.AnyStr, func: typing.Function) -> None: # However, typing.Function does not exist. # How can I specify the type function for the parameter `func`? # do some processing pass I checked PEP 483 , but could not find a function type hint there. Jim Fasarakis Hilliard As @jonrsharpe noted in a comment, this can be done with typing.Callable : from typing import AnyStr, Callable def my_function

How to use type hints in python 3.6?

半城伤御伤魂 提交于 2019-11-26 09:36:13
问题 I noticed python 3.5 and python 3.6 added a lot of features about static type checking, so I tried with the following code(in python 3.6, stable version). from typing import List a: List[str] = [] a.append(\'a\') a.append(1) print(a) What surprised me was that, python didn\'t give me an error or warning, although 1 was appended to a list which should only contain strings. Pycharm detected the type error and gave me a warning about it, but it was not obvious and it was not shown in the output

How can I specify the function type in my type hints?

早过忘川 提交于 2019-11-26 02:37:32
问题 I want to use type hints in my current Python 3.5 project. My function should receive a function as parameter. How can I specify the type function in my type hints? import typing def my_function(name:typing.AnyStr, func: typing.Function) -> None: # However, typing.Function does not exist. # How can I specify the type function for the parameter `func`? # do some processing pass I checked PEP 483, but could not find a function type hint there. 回答1: As @jonrsharpe noted in a comment, this can be