KEMBAR78
Failing to infer type upon concretization of generic (inferred <nothing>) · Issue #14785 · python/mypy · GitHub
Skip to content

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

@magwas

Description

@magwas

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions