python / python/mypy

[Feature] Get the type of generic typeVar in class

Open
#13,951 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

Having a class inheriting from a Generic make it possible to get type of the typeVar, possibly with a method like get_type or alike.

class MyClass(Generic[T]):
    def __init__(self):
         self.generic_type = T.get_type()

Pitch

Other languages' type system have this capability.

e.g.

C#;
Type type = abc.GetType().GetGenericArguments()[0];
https://stackoverflow.com/questions/557340/how-to-get-the-type-of-t-from-a-member-of-a-generic-class-or-method

C++;

template <class T>
class MyClass : public Base<T>
{
public:
    T t;
    decltype(t) t2;
};

https://en.cppreference.com/w/cpp/language/decltype

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

No files, tests, or entry points are identified in the issue, so begin by locating mypy's handling of Generic classes and type variables. Done would require an agreed design and implementation that lets a generic class obtain its type variable's type, with tests demonstrating the requested behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.