Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upRecord Types #685
Record Types #685
Comments
|
This was discussed during one of last typing meet-ups, see at the bottom of summary. I proposed to call these key types (essentially they will act as string generic, similar to integer generics). The syntax I tend to like the most is something like this for attributes (proposed by Guido IIRC): K = Key("K")
class Proxy(Generic[T]):
def __init__(self, target: T):
self.target = target
def __getattr__(self, name: K) -> T.K:
return self.target.nameand Anyway, IMO the question of syntax is not important an this point. This topic (support for static typing of Python numeric stack) is obviously important (but gets constantly deprioritized for mypy team). Also I would say we should start with integer generic (shape types), and string generic (key types) will follow next. See also an essentially the same proposal on mypy tracker python/mypy#7856 |
Yeah that's why I avoided it. Do you have a sense if something like this could be first written as a MyPy extension before integrated into core, so we could start building/playing with it? |
Unfortunately, I don't think it is possible, it requires changes in some very deep parts of mypy. |
Sounds exciting! :) Well if I end up having bandwidth to work on this I will start poking around and asking more questions. |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

I would like to be able to type a Dataframe like object with MyPy, where different columns have different types and you can get each as column as an attribute on the dataframe. This is how libraries like Pandas and Ibis work.
Generally, this requires a function to return different types by mapping string literals to different types (record kinds).
Here is a mock example implemented in Typescript, which checks properly:
Possible Syntaxes
Here are a few possible ways this could be spelled in Python:
selfasTypedDictSince we already have a
TypedDictconstruct one of the least invasive approaches is to typeselfas aTypeDict.This would probably require anonymous
TypeDicts, which was proposed previously (python/mypy#985 (comment)).It would also required
TypedDicts to be able to take generic parameters.Type Level
.keysand__getitem__Another option would be to mirror how Typescript does this, by introducing type level
keysand__gettitem__functions. This would also require generic to depend on other generics (python/mypy#2756).Conclusion
I would like to have a way to type Dataframes that have different column types in a generic way. This is useful for typing frameworks like Ibis or Pandas.
This is somewhat related to variadic generics I believe (#193). Also related: dropbox/sqlalchemy-stubs#69