Skip to content

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

Closed
@glyph

Description

@glyph

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 :).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions