Skip to content

Instantly share code, notes, and snippets.

@Gobot1234
Last active December 16, 2023 20:03
Show Gist options
  • Save Gobot1234/4683b61800769a681d96cae70ece2366 to your computer and use it in GitHub Desktop.
Save Gobot1234/4683b61800769a681d96cae70ece2366 to your computer and use it in GitHub Desktop.
Typing Features I'd like to see/write a PEP for

Generalising currently special cased types

TypedMapping mixin

A generic version of typing.TypedDict.

This would allow for people to make there own typed Mappings, it would desugar to a series of overloads on __getitem__ and get

class MyMapping(MultiDict, TypedMapping):
  foo: int
  bar: NotRequired[str]

A type checker should see this as (but in valid typed python)

class MyMapping(MultiDict):
    def __getitem__(self, key: "foo") -> int: ...
    def __getitem__(self, key: "bar") -> str | Never: ...
    def get(self, key: "foo", default: Never = ...) -> int: ...
    def get(self, key: "bar", default: T = ...) -> str | T: ...

This would make this whole file not a big # type: ignore along with not working on mypy.

TypedDict would be defined as

class TypedDict(Mapping[str, Any], TypedMapping):
    pass

PhantomType

A generic version of typing.Annotated. It erases its first X number of types at runtime using Varidic types.

Annotated would be defined like so

class Annotated(PhantomType[T, *Ts]):
    __metadata__: tuple[*Ts]

Better runtime typing support

function.__getitem__

TypeVarLike default substitution

Getting the substituted TypeVarLike from a parameter list T.__value__

types.NotType

This is an exclusion type and would have to be added after an intersection type.

def ask_for_input(to_print: str & ~Literal[""]) -> str: ...

def fn(x: ~Literal[None]): ...

class Foo:
    def __eq__(self, other: ~Self) -> Literal[False]: ...
    def __eq__(self, other: Self) -> bool: ...
    def __eq__(self, other: object) -> bool: 
        return isinstance(other, self.__class__) and ...

TODO

IntersectionType

Higher Kinded TypeVars (HKT)

Lazy typing imports

type import typing

class Foo:
    bar: typing.Dict  # for lack of a better symbol coming to mind

Foo.__annotations__  # calls __annotate__() and annotate can lazyily get the typing import 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment