Skip to content
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

Better type annotations for each_record() #6

Open
multimeric opened this issue Aug 3, 2022 · 1 comment
Open

Better type annotations for each_record() #6

multimeric opened this issue Aug 3, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@multimeric
Copy link
Owner

This might be tricky, since each overload depends on the instance variables, so @overload can't be used here. This may require a small API restructure.

@multimeric multimeric added the enhancement New feature or request label Aug 3, 2022
@multimeric
Copy link
Owner Author

multimeric commented May 18, 2024

I don't think the Python type system is flexible enough to indicate that the return value of each_record() depends on the value of the fields argument. The most sensible option is therefore to have the return value be a TypedDict that contains all possible keys, since this is the most typical usage.

It might be possible to do some hackery where search becomes a generic method using the new Py3.12 generic methods syntax. But then to get the correct functionality, the user would have to pass in the entire return type dictionary as T, which misses the user friendliness. They could pass in a union T that includes all possible fields, which becomes the keys of the return dictionary dynamically (dict[T, Any]), but then we lose the specific types of the values themselves. Also in both of these cases, Python probably can't check that the passed in T is a true subset of all the legal return values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant