Skip to content

with-syntax makes mypy confuse int with Any #9750

Closed
@Hi-Angel

Description

@Hi-Angel

Bug Report

When an object is declared with with syntax, and then a method of that object is called, mypy thinks that method returns Any, even if the method is properly annotated to return something else. Removing with and declaring the object explicitly makes it work.

To Reproduce (in terms of terminal commands)

 λ cat test.py
class Connection:
    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        pass

    def register_method(self) -> int:
        return 1

def foo() -> int:
    with Connection() as conn:
        return conn.register_method()
 λ mypy test.py --warn-return-any
test.py:13: error: Returning Any from function declared to return "int"
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

mypy would return no errors.

Actual Behavior

mypy returned errors.

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: --warn-return-any
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.6
  • Operating system and version: Archlinux

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions