python / python/mypy

Generic method returning nested class

Open
#6,689 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

  • Are you reporting a bug, or opening a feature request? bug

  • Please insert below the code you are checking with mypy.

    from typing import Generic, TypeVar
    
    T = TypeVar('T', bound='Base')
    
    class Base:
        class Inner: ...
    
    class A(Base):
        class Inner: ...
    
    class Foo(Generic[T]):
        def func(self) -> T.Inner: ...
    
    foo = Foo[A]()
    reveal_type(foo.func())
    
  • What is the actual behavior/output?

    main.pyi:15: error: Revealed type is 'main.A*'
    
  • What is the behavior/output you expect?

    main.pyi:15: error: Revealed type is 'main.A.Inner*'
    

    If the analyzer is not sophisticated enough to figure this out yet, it should raise an error/warn and turn into <nothing> rather than becoming the wrong type.

  • What are the versions of mypy and Python you are using? mypy v0.701 + CPython v3.7.1
    Do you see the same issue after installing mypy from Git master? yes, same results with 0.710+dev.bb7dbd5afd84656a62311e5f69a1cef6d06466bc

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

Start with the reproducer in main.pyi, especially the generic Foo.func return annotation and the reveal_type call at line 15. Trace how the type checker resolves T.Inner for Foo[A]; done means it reports A.Inner rather than A, or emits an error and produces no type.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.