-
Notifications
You must be signed in to change notification settings - Fork 257
Runtime implementation of TypedDict #322
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
Conversation
@gvanrossum Important comment: the iterable syntax supports arbitrary iterables, including both list of tuples and a dictionary. I edited the description to clarify this. |
FYI I think we should not put this in typing.py yet. The plan for TypedDict is to use the new mypy_extensions machanism first. |
I see. So should I close it here and instead make a PR to mypy? As I understand, the runtime implementation will be needed anyway. Or is it supposed to be a typecheck only extension? (Sorry I was not following this idea closely.) |
OOI, why isn't the runtime implementation of |
For design questions it's probably better to address these to the issue in mypy: python/mypy#985 or perhaps the corresponding issue here in typing: #28 I think Mark's POV has value: after |
If a typed dictionary type object would be an alias for |
Indeed,
class Point1D(TypedDict):
x: float
class Point2D(Point1D):
y: float
class Point3D(Point2D):
z: float then |
Indeed I was thinking |
Hi @ilevkivskyi, thanks for taking a sizeable stab at the runtime syntax implementation! Were you planning on migrating this PR to mypy_extensions? I'm personally most interested in incremental support for keyword-args syntax (i.e. I'm holding off on support for the class-based syntax in mypy for a while until issues in the initial implementation are shaken out. This doesn't preclude you adding runtime support for the class-based syntax. It just means it won't be recognized in mypy until I or someone else gets around to implementing type-checker support for it, which might be a while. :-) |
@davidfstr Yes, I was going to move this to mypy_extensions, also I could implement the support for class-based syntax in mypy after everything else is settled (same way as I did this for |
I am closing this in favour of python/mypy#2552 |
This was initially proposed in python/typing#322. It works on Python 2 and 3.
This was initially proposed in python/typing#322. It works on Python 2 and 3.
Fixes #28
@gvanrossum I think it is time to consider the runtime implementation of
TypedDict
(mypy counterpart seems to be ready soon). Here is a simple (but quite flexible) implementation that supports three forms (iterable, keywords, class for 3.6+). In all forms type info for runtime introspection is accessible via__annotations__
, multiple inheritance is supported. Some examples:Everything is implemented to be
pickle
able and fast. I have measured andPoint2D(x=1, y=2)
is slower thatdict(x=1, y=2)
by only 3% to 11% depending on Python version.Please, don't be scared by the size of diff. more than half of it is tests and implementations for Python 2 and Python 3 are identical.