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

Failing to infer type upon concretization of generic (inferred <nothing>) #14785

Open
magwas opened this issue Feb 26, 2023 · 0 comments
Open
Labels
bug mypy got something wrong topic-type-variables

Comments

@magwas
Copy link

magwas commented Feb 26, 2023

Bug Report

I created a generic class (Baz) which except an instance with a generic interface (Foo), and manipulates it.

I made a non-generic class (Bar), subclassing the interface with a concrete type (str).

Inside the class the types work, but when I use the non-generic class with the manipulator class, I have type errors.

I expected mypy to infer the concretization of the generic type from the usage.

I have confirmation that my typing is indeed correct: https://stackoverflow.com/questions/75568405/typing-of-using-a-subclass-of-a-generic-class?noredirect=1#comment133323604_75568405

To Reproduce


from typing import Generic, TypeVar


T = TypeVar("T")


class Foo(Generic[T]):
    def foo(self, arg: T) -> None:
        pass


class Bar(Foo[str]):
    def foo(self, arg: str) -> None:
        print(arg + " boldly runs")


T1 = TypeVar("T1")


class Baz(Generic[T1]):
    def sirRobin(self, instance: Foo[T1], param: T1) -> None:
        instance.foo(param)


# no problem, no output
Baz[str]().sirRobin(Foo(), "she")
Baz[int]().sirRobin(Foo(), 1)
print("----")
# Argument 1 to "sirRobin" of "Baz" has incompatible type "Bar"; expected "Foo[<nothing>]"  [arg-type]
# Argument 2 to "sirRobin" of "Baz" has incompatible type "str"; expected < nothing > [arg-type]
# works as expected though
Baz().sirRobin(Bar(), "he")

Playground url: https://mypy-play.net/?mypy=latest&python=3.11&gist=7bdb14547c7d7ea11185c02e6267c016

Expected Behavior
Correctly infer that T=str
No errors

Actual Behavior

Inferred T = nothing

Argument 1 to "sirRobin" of "Baz" has incompatible type "Bar"; expected "Foo[]" [arg-type]
Argument 2 to "sirRobin" of "Baz" has incompatible type "str"; expected < nothing > [arg-type]

Your Environment

  • Mypy version used: latest
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
strict = True
explicit_package_bases = True
namespace_packages = True
#mypy_path = src:test
files = src,test
mypy_path = $MYPY_CONFIG_FILE_DIR/src:$MYPY_CONFIG_FILE_DIR/test

  • Python version used: 3.11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-variables
Projects
None yet
Development

No branches or pull requests

2 participants