-
Notifications
You must be signed in to change notification settings - Fork 242
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
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.name and 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. |
Any updates on this? Its been a year and I really need the keyof alternative in python |
Sorry, nothing yet. Can you describe your application in more detail? |
Here's an example use case: I have a DataFrame I'd like to be able to more precisely type the DataFrame as being TypedDict-ish with its columns, and then type Or put otherwise:
|
That sounds like you just want |
I opened a discussion with a syntax proposal and some examples: #1387 |
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:
self
asTypedDict
Since we already have a
TypedDict
construct one of the least invasive approaches is to typeself
as aTypeDict
.This would probably require anonymous
TypeDict
s, which was proposed previously (python/mypy#985 (comment)).It would also required
TypedDict
s to be able to take generic parameters.Type Level
.keys
and__getitem__
Another option would be to mirror how Typescript does this, by introducing type level
keys
and__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
The text was updated successfully, but these errors were encountered: