python / python/mypy

Lambda as RHS for operator always gets inferred as Any

Open
#5,843 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-2-low topic-disallow-any
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

When a lambda is given as the RHS in an operator (example uses >>, but also occurs with others), the lambda’s type is always inferred as Any. Whereas calling the operator function directly, or passing an explicit function, does not.

from typing import Callable, Generic, TypeVar

_T = TypeVar('_T')
_U = TypeVar('_U')

class A(Generic[_T]):
    _value: _T
    def __init__(self, val: _T) -> None: ...
    def __rshift__(self, f: Callable[[_T], _U]) -> _U: ...

def inc(x: int) -> int: ...

A(1) >> inc  # No error
A(1).__rshift__(lambda x: x + 1)  # No error
A(1) >> (lambda x: x + 1)
#        │         └ Expression has type "Any"
#        └ Expression type contains "Any" (has type Callable[[Any], Any])
  • Python 3.7.0
  • mypy 0.641
  • mypy config:
    [mypy]
    check_untyped_defs = True
    disallow_any_decorated = True
    disallow_any_expr = True
    disallow_any_unimported = True
    disallow_incomplete_defs = True
    disallow_subclassing_any = True
    disallow_untyped_calls = True
    disallow_untyped_decorators = True
    disallow_untyped_defs = True
    warn_return_any = True
    warn_unused_ignores = True
    

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the supplied Python reproduction with the listed mypy configuration and compare the operator form with the direct method call and named function. Trace the operator-expression type inference from that entry point; done means the parenthesized lambda is inferred from the operand type instead of producing Any.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.