Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions mypy_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,37 @@
from mypy_extensions import TypedDict
"""

from typing import Any, Dict
from typing import Any, Callable, Dict, Final, Literal, TypeVar

import sys
# _type_check is NOT a part of public typing API, it is used here only to mimic
# the (convenient) behavior of types provided by typing module.
from typing import _type_check # type: ignore
from typing import _type_check # type: ignore [attr-defined]

from typing_extensions import NotRequired, TypeGuard, Unpack


_C = TypeVar("_C", bound=Callable[..., Any])

MYPYC_ATTRS: Final = frozenset([
"native_class",
"allow_interpreted_subclasses",
"serializable",
"free_list_len",
])

MypycAttr = Literal[
"native_class",
"allow_interpreted_subclasses",
"serializable",
"free_list_len",
]

class MypycAttrs(TypedDict):
native_class: NotRequired[bool]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there others? I was looking thru the docs and couldn't find any good central resource on this

allow_interpreted_subclasses: NotRequired[bool]
serializable: NotRequired[bool]
free_list_len: NotRequired[int]


def _check_fails(cls, other):
Expand Down Expand Up @@ -158,12 +183,25 @@ def trait(cls):
return cls


def mypyc_attr(*attrs, **kwattrs):
def _validate_mypyc_attr_key(key: str) -> TypeGuard[MypycAttr]:
if key not in MYPYC_ATTRS:
raise ValueError(
f"{key!r} is not a valid `mypyc_attr` key.\n"
"Valid keys are: {', '.join(map(repr, sorted(MYPYC_ATTRS)))}"
)


def mypyc_attr(*attrs: MypycAttr, **kwattrs: Unpack[MypycAttrs]) -> Callable[[_C], _C]:
for key in attrs:
_validate_mypyc_attr_key(key)
for key, value in kwattrs.items():
_validate_mypyc_attr_key(key)
if not isinstance(value, bool):
raise TypeError(f"{key} value should be boolean, not {type(value).__name__}.")
return lambda x: x


# TODO: We may want to try to properly apply this to any type
# variables left over...
# TODO: We may want to try to properly apply this to any type variables left over...
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this just looked silly as a multiline comment once the block above was added

class _FlexibleAliasClsApplied:
def __init__(self, val):
self.val = val
Expand Down