Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot infer (or provide) types of lambdas #4226

Open
bwo opened this issue Nov 8, 2017 · 6 comments
Open

cannot infer (or provide) types of lambdas #4226

bwo opened this issue Nov 8, 2017 · 6 comments

Comments

@bwo
Copy link
Contributor

bwo commented Nov 8, 2017

Consider this file: https://gist.github.com/bwo/077400d43c517ca0b80f2e85f615073e
The output from running mypy is:

comp.py:57: error: Cannot infer type argument 1 of "cfilter"
comp.py:61: error: Revealed type is 'typing.Sequence*[Any]'
comp.py:67: error: Cannot infer type argument 1 of "cmap"
comp.py:68: error: Revealed type is 'def (typing.Sequence*[Any]) -> typing.Sequence*[builtins.int*]'
comp.py:70: error: Revealed type is 'typing.Sequence*[builtins.int*]'
comp.py:72: error: Cannot infer type argument 3 of "comp"

I suspect this is related to the fact that reveal_type(len) and reveal_type(lambda s: len(s)) are, to my surprise, not the same (the latter accepts Any as input, the former requires Sized). According to mypy docs lambdas are subject to bidirectional typechecking, but afaict sufficient information is present in the file given to type the lambdas; nevertheless, they aren't.

@gvanrossum
Copy link
Member

gvanrossum commented Nov 8, 2017 via email

@bwo
Copy link
Contributor Author

bwo commented Nov 8, 2017

Replacing the partial calls with lambdas (or named functions) doesn't change the output. Also, I'm only using partial in the implementation of the type-overloaded functions, which I thought mypy wouldn't check, and the same inference failure occurs just with comp(lambda x: x + 1, lambda s: len(s)).

@gvanrossum
Copy link
Member

gvanrossum commented Nov 9, 2017 via email

@bwo
Copy link
Contributor Author

bwo commented Nov 9, 2017

from typing import Callable, TypeVar

S = TypeVar('S')
T = TypeVar('T')
U = TypeVar('U')


def comp(g, f):
    # type: (Callable[[T], U], Callable[[S], T]) -> Callable[[S], U]
    def composed(x):
        # type : (S) -> U
        return g(f(x))
    return composed


def test(s):
    # type: (str) -> int

    len_and_inc = comp(lambda i: i + 1, lambda s: len(s))
    x = len_and_inc(s)
    reveal_type(x)
    return x

revealed type is Any. Replacing lambda s: len(s) with len reveals a type of int. Replacing len_and_inc = comp(lambda i: i+ 1, lambda s: len(s)) with just len_and_inc = lambda s: len(s) reveals a type of int as well.

@gvanrossum
Copy link
Member

gvanrossum commented Nov 9, 2017 via email

miketheman added a commit to miketheman/warehouse that referenced this issue Mar 7, 2022
See python/mypy#4226

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
miketheman added a commit to miketheman/warehouse that referenced this issue Mar 7, 2022
See python/mypy#4226

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
di pushed a commit to pypi/warehouse that referenced this issue Mar 9, 2022
* chore(deps): install mypy in lint.in

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* chore(deps): include more types-* packages for mypy

These were suggested from running mypy on the codebase.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* chore: configure mypy

Set configuration for mypy.

Exclude some of the subdirectories we are not interested in testing to
speed up mypy execution.

Ignore any 3rd party modules that we do not have types for yet.
Added links that I could find to help track completion.

Does **not** set `strict` mode yet, since that's a bigger lift.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* chore: add mypy runner script

Eventually this command should fold into `bin/lint` and be removed.

For now, it's a convenient execution wrapper.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: ignore dynamic properties

Callables are receiving dynamic attributes, something that isn't "usual".

See python/mypy#708

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: ignore lambdas

See python/mypy#4226

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: ignore hybrid_property repeat definitions

Part of the SQLAlchemy extensions, which do not yet have reliable
stubs/plugins.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: ignore sqlalchemy declarative

Should come along with sqlalchemy stubs.

See: https://docs.sqlalchemy.org/en/14/orm/extensions/mypy.html#using-declared-attr-and-declarative-mixins

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: a few more ignores

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: interface methods shouldn't use self

Surfaced via mypy, corrected!

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: correct subclass path

Surfaced via mypy, corrected.

Unclear why this wouldn't have been caught by other tools.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: rename internal variable

mypy detected this as a type mismatch, as the internal variable name was
shadowing the externally-supplied one, and changing the type.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: ignore flake8 line too long for ignored types

Adding a `# type: ignore` comment to a couple of places triggered
flake8's line too long check.

Running `make reformat` did nothing for these - black has outstanding
design issues with line length and comments.

See psf/black#1713 for one example.

Instead of changing the line structure to accommodate, ignore these two
cases, at least until the types can be fixed and the comments removed.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* Revert "chore: add mypy runner script"

This reverts commit fffeadb.

* test: include mypy in lint execution

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* chore(deps): include itsdangerous type stubs

Until itsdangerous 2.0 is included, this types package is needed.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
domdfcoding pushed a commit to domdfcoding/warehouse that referenced this issue Jun 7, 2022
* chore(deps): install mypy in lint.in

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* chore(deps): include more types-* packages for mypy

These were suggested from running mypy on the codebase.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* chore: configure mypy

Set configuration for mypy.

Exclude some of the subdirectories we are not interested in testing to
speed up mypy execution.

Ignore any 3rd party modules that we do not have types for yet.
Added links that I could find to help track completion.

Does **not** set `strict` mode yet, since that's a bigger lift.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* chore: add mypy runner script

Eventually this command should fold into `bin/lint` and be removed.

For now, it's a convenient execution wrapper.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: ignore dynamic properties

Callables are receiving dynamic attributes, something that isn't "usual".

See python/mypy#708

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: ignore lambdas

See python/mypy#4226

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: ignore hybrid_property repeat definitions

Part of the SQLAlchemy extensions, which do not yet have reliable
stubs/plugins.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: ignore sqlalchemy declarative

Should come along with sqlalchemy stubs.

See: https://docs.sqlalchemy.org/en/14/orm/extensions/mypy.html#using-declared-attr-and-declarative-mixins

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: a few more ignores

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: interface methods shouldn't use self

Surfaced via mypy, corrected!

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: correct subclass path

Surfaced via mypy, corrected.

Unclear why this wouldn't have been caught by other tools.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: rename internal variable

mypy detected this as a type mismatch, as the internal variable name was
shadowing the externally-supplied one, and changing the type.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* lint: ignore flake8 line too long for ignored types

Adding a `# type: ignore` comment to a couple of places triggered
flake8's line too long check.

Running `make reformat` did nothing for these - black has outstanding
design issues with line length and comments.

See psf/black#1713 for one example.

Instead of changing the line structure to accommodate, ignore these two
cases, at least until the types can be fixed and the comments removed.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* Revert "chore: add mypy runner script"

This reverts commit fffeadb.

* test: include mypy in lint execution

Signed-off-by: Mike Fiedler <miketheman@gmail.com>

* chore(deps): include itsdangerous type stubs

Until itsdangerous 2.0 is included, this types package is needed.

Signed-off-by: Mike Fiedler <miketheman@gmail.com>
@ilevkivskyi
Copy link
Member

FWIW on current master mypy gives a very different output:

test.py:57: error: Unsupported operand types for > ("int" and "T")  [operator]
test.py:61: note: Revealed type is "typing.Sequence[builtins.int]"
test.py:67: error: Argument 1 to "len" has incompatible type "T"; expected "Sized"  [arg-type]
test.py:68: note: Revealed type is "def [T] (typing.Sequence[T`22]) -> typing.Sequence[builtins.int]"
test.py:70: note: Revealed type is "typing.Sequence[builtins.int]"
test.py:72: error: Argument 1 to "len" has incompatible type "S"; expected "Sized"  [arg-type]

The current errors look much better, and it looks like they can be fixed by adding corresponding upper bounds to type variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants