Python typing: declare the type of the called when to give it an instance method

Consider the following code:

import typing

def a(x: int, y: int) -> int:
    return x + y

class F(object):
    def b(self, x: int, y: int) -> int:
        return x + y

def call(operation: typing.Callable[[int, int], int]) -> int:
    return operation(2, 2)

call(a)

f = F()
call(f.b)

My PyCharm IDE indicates an input error for the last line:

1

Is this a type / type declaration error? Does type checking work like PyCharm? If this is an input error, what should it be?

+4
source share
1 answer

This is a PyCharm typechecker error. mypy typechecker takes your example without warning or error:

$ bin/mypy --verbose so_41869174.py
LOG:  Mypy version 0.470
LOG:  Parsing so_41869174.py (so_41869174)
LOG:  Parsing lib/mypy/typeshed/stdlib/3/typing.pyi (typing)
LOG:  Parsing lib/mypy/typeshed/stdlib/3/builtins.pyi (builtins)
LOG:  Parsing lib/mypy/typeshed/stdlib/3/sys.pyi (sys)
LOG:  Parsing lib/mypy/typeshed/stdlib/3/abc.pyi (abc)
LOG:  Parsing lib/mypy/typeshed/stdlib/3/types.pyi (types)
LOG:  Parsing lib/mypy/typeshed/third_party/2and3/mypy_extensions.pyi (mypy_extensions)
LOG:  Parsing lib/mypy/typeshed/stdlib/3/_importlib_modulespec.pyi (_importlib_modulespec)
LOG:  Loaded graph with 8 nodes
LOG:  Found 2 SCCs; largest has 7 nodes
LOG:  Processing SCC of size 7 (_importlib_modulespec mypy_extensions types abc typing sys builtins) as inherently stale
LOG:  Processing SCC singleton (so_41869174) as inherently stale
LOG:  No fresh SCCs left in queue
LOG:  Build finished in 0.482 seconds with 8 modules, 1708 types, and 0 errors

Since it F().bis a bound method, it inherits the signature of the base function without an argument self(since this job of the bound method is passed in the bound instance).

, typing.get_type_hints(), bound, self:

>>> typing.get_type_hints(f.b)
{'x': <class 'int'>, 'y': <class 'int'>, 'return': <class 'int'>}
+2

Source: https://habr.com/ru/post/1667850/


All Articles