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

every-attribute-optional derived type, like TypeScript's Partial #10889

Closed
glyph opened this issue Jul 29, 2021 · 1 comment
Closed

every-attribute-optional derived type, like TypeScript's Partial #10889

glyph opened this issue Jul 29, 2021 · 1 comment
Labels

Comments

@glyph
Copy link

glyph commented Jul 29, 2021

Feature

In TypeScript you can say Partial<X> and get an object whose attributes are all Optional; I would like this in some form for Mypy.

See https://www.typescriptlang.org/docs/handbook/utility-types.html

Pitch

This is incredibly useful for any situation where you need a delta, or a partially-constructed / curried form of an object. For example, when processing a PATCH request in an HTTP framework, a Partial object lets you avoid re-defining a class or working with unstructured JSON because you already know all the attributes you might receive and now you can just test them for None-ness. Or in a test framework where 90% of the attributes are initialized with the same defaults, you can provide a function that takes a partial type which populates only the attributes which are unspecified.

The absence of object literals makes this slightly less useful in Python, but maybe if we added **kw to object.__init__ to assign as attributes, it would be even better :).

@glyph glyph added the feature label Jul 29, 2021
@AlexWaygood
Copy link
Member

I'm closing this, as it's a feature request for the typing system in general, rather than a feature request for mypy specifically. Proposals like this should go to the https://github.com/python/typing repo or the typing-sig mailing list.

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

No branches or pull requests

2 participants