From 56dfc950dad23adb1735f6e244b54267d3b9d96c Mon Sep 17 00:00:00 2001 From: David Ellis Date: Fri, 26 Jan 2024 12:58:23 +0000 Subject: [PATCH] Add basic stub file for __init__.py (#80) * Delete old, outdated stub files. * Add py.typed and very basic stub so tools can know what they're importing * Bump version number --- src/prefab_classes/__init__.py | 4 +- src/prefab_classes/__init__.pyi | 18 ++ src/prefab_classes/py.typed | 1 + stubs/WARNING.txt | 2 - stubs/prefab_classes/__init__.pyi | 8 - stubs/prefab_classes/_typing.pyi | 8 - stubs/prefab_classes/compiled/__init__.pyi | 6 - stubs/prefab_classes/compiled/generator.pyi | 166 ------------------ .../compiled/rewrite_source.pyi | 12 -- stubs/prefab_classes/constants.pyi | 10 -- stubs/prefab_classes/dynamic/__init__.pyi | 5 - .../dynamic/_attribute_class.pyi | 31 ---- stubs/prefab_classes/dynamic/autogen.pyi | 7 - .../dynamic/method_generators.pyi | 25 --- stubs/prefab_classes/dynamic/prefab.pyi | 106 ----------- stubs/prefab_classes/exceptions.pyi | 4 - stubs/prefab_classes/funcs.pyi | 16 -- stubs/prefab_classes/hook.pyi | 25 --- stubs/prefab_classes/sentinels.pyi | 7 - 19 files changed, 21 insertions(+), 440 deletions(-) create mode 100644 src/prefab_classes/__init__.pyi create mode 100644 src/prefab_classes/py.typed delete mode 100644 stubs/WARNING.txt delete mode 100644 stubs/prefab_classes/__init__.pyi delete mode 100644 stubs/prefab_classes/_typing.pyi delete mode 100644 stubs/prefab_classes/compiled/__init__.pyi delete mode 100644 stubs/prefab_classes/compiled/generator.pyi delete mode 100644 stubs/prefab_classes/compiled/rewrite_source.pyi delete mode 100644 stubs/prefab_classes/constants.pyi delete mode 100644 stubs/prefab_classes/dynamic/__init__.pyi delete mode 100644 stubs/prefab_classes/dynamic/_attribute_class.pyi delete mode 100644 stubs/prefab_classes/dynamic/autogen.pyi delete mode 100644 stubs/prefab_classes/dynamic/method_generators.pyi delete mode 100644 stubs/prefab_classes/dynamic/prefab.pyi delete mode 100644 stubs/prefab_classes/exceptions.pyi delete mode 100644 stubs/prefab_classes/funcs.pyi delete mode 100644 stubs/prefab_classes/hook.pyi delete mode 100644 stubs/prefab_classes/sentinels.pyi diff --git a/src/prefab_classes/__init__.py b/src/prefab_classes/__init__.py index 772fc9e..e5e037e 100644 --- a/src/prefab_classes/__init__.py +++ b/src/prefab_classes/__init__.py @@ -17,8 +17,8 @@ "is_prefab_instance", ] -__version__ = "v0.10.0" -PREFAB_MAGIC_BYTES = b"PREFAB_CLASSES_v0.10.0" +__version__ = "v0.10.1" +PREFAB_MAGIC_BYTES = b"PREFAB_CLASSES_v0.10.1" _imports = [ MultiFromImport(".dynamic", ["prefab", "attribute", "build_prefab"]), diff --git a/src/prefab_classes/__init__.pyi b/src/prefab_classes/__init__.pyi new file mode 100644 index 0000000..187d438 --- /dev/null +++ b/src/prefab_classes/__init__.pyi @@ -0,0 +1,18 @@ +from .dynamic import prefab, attribute, build_prefab +from .sentinels import KW_ONLY +from .hook import prefab_compiler +from .funcs import is_prefab, is_prefab_instance + +__version__: str +PREFAB_MAGIC_BYTES: bytes + +__all__: list[str] = [ + "__version__", + "prefab", + "attribute", + "build_prefab", + "KW_ONLY", + "prefab_compiler", + "is_prefab", + "is_prefab_instance", +] diff --git a/src/prefab_classes/py.typed b/src/prefab_classes/py.typed new file mode 100644 index 0000000..b648ac9 --- /dev/null +++ b/src/prefab_classes/py.typed @@ -0,0 +1 @@ +partial diff --git a/stubs/WARNING.txt b/stubs/WARNING.txt deleted file mode 100644 index f3d7903..0000000 --- a/stubs/WARNING.txt +++ /dev/null @@ -1,2 +0,0 @@ -These type stubs are incomplete and probably incorrect. -They are not currently included with the package as they may be more harmful than helpful. diff --git a/stubs/prefab_classes/__init__.pyi b/stubs/prefab_classes/__init__.pyi deleted file mode 100644 index 4bda362..0000000 --- a/stubs/prefab_classes/__init__.pyi +++ /dev/null @@ -1,8 +0,0 @@ -from .dynamic import ( - attribute as attribute, - build_prefab as build_prefab, - prefab as prefab, -) -from .sentinels import KW_ONLY as KW_ONLY - -PREFAB_MAGIC_BYTES: bytes diff --git a/stubs/prefab_classes/_typing.pyi b/stubs/prefab_classes/_typing.pyi deleted file mode 100644 index 114355a..0000000 --- a/stubs/prefab_classes/_typing.pyi +++ /dev/null @@ -1,8 +0,0 @@ -def dataclass_transform( - *, - eq_default: bool = ..., - order_default: bool = ..., - kw_only_default: bool = ..., - field_specifiers: tuple = ..., - **kwargs, -): ... diff --git a/stubs/prefab_classes/compiled/__init__.pyi b/stubs/prefab_classes/compiled/__init__.pyi deleted file mode 100644 index aa7836e..0000000 --- a/stubs/prefab_classes/compiled/__init__.pyi +++ /dev/null @@ -1,6 +0,0 @@ -from .rewrite_source import ( - get_sources_to_compare as get_sources_to_compare, - preview as preview, - rewrite_code as rewrite_code, - rewrite_to_py as rewrite_to_py, -) diff --git a/stubs/prefab_classes/compiled/generator.pyi b/stubs/prefab_classes/compiled/generator.pyi deleted file mode 100644 index 833df36..0000000 --- a/stubs/prefab_classes/compiled/generator.pyi +++ /dev/null @@ -1,166 +0,0 @@ -import ast -from ..constants import ( - ATTRIBUTE_FUNCNAME as ATTRIBUTE_FUNCNAME, - CLASSVAR_NAME as CLASSVAR_NAME, - COMPILED_FLAG as COMPILED_FLAG, - COMPILE_ARGUMENT as COMPILE_ARGUMENT, - DECORATOR_NAME as DECORATOR_NAME, - FIELDS_ATTRIBUTE as FIELDS_ATTRIBUTE, - POST_INIT_FUNC as POST_INIT_FUNC, - PREFAB_INIT_FUNC as PREFAB_INIT_FUNC, - PRE_INIT_FUNC as PRE_INIT_FUNC, -) -from ..dynamic import prefab as prefab -from ..exceptions import CompiledPrefabError as CompiledPrefabError -from _typeshed import Incomplete -from functools import cached_property -from typing import Union - -assignment_type: str -prefab_essentials: set[str] - -class Field: - name: str - field: Union[ast.AnnAssign, ast.Assign] - default: Union[None, ast.expr] - default_factory: Union[None, ast.expr] - init_: bool - repr_: bool - kw_only: bool - exclude_field: bool - annotation: Union[None, ast.expr] - attribute_func: bool - def __prefab_post_init__(self) -> None: ... - - @cached_property - def default_factory_call(self) -> ast.Call: ... - - def ast_attribute( - self, obj_name: str = ..., ctx: Union[type[ast.Load], type[ast.Store]] = ... - ): ... - @classmethod - def from_keywords( - cls, name, field, keywords, annotation: Incomplete | None = ... - ): ... - -class PrefabDetails: - name: str - node: ast.ClassDef - decorator: ast.Call - init: bool - repr: bool - eq: bool - iter: bool - match_args: bool - kw_only: bool - frozen: bool - compile_prefab: bool - compile_plain: bool - compile_fallback: bool - compile_slots: bool - - def __prefab_post_init__(self) -> None: ... - - @property - def field_names(self): ... - - @property - def field_list(self): ... - - @property - def resolved_field_names(self): ... - - @property - def resolved_field_list(self): ... - - def gather_methods_arguments(self) -> None: ... - - @property - def defined_attr_names(self): ... - - @property - def func_arguments(self): ... - - @property - def resolved_func_arguments(self) -> dict[str, list[str]]: ... - - @staticmethod - def call_method( - method_name, *, args: Incomplete | None = ..., keywords: Incomplete | None = ... - ): ... - - @property - def pre_init_call(self): ... - - @property - def post_init_call(self): ... - - @property - def ast_qualname_str(self): ... - - @cached_property - def fields(self) -> dict[str, Field]: ... - - @cached_property - def parents(self) -> list[str]: ... - - def resolve_inheritance(self, prefabs: dict[str, "PrefabDetails"]): ... - - @property - def resolved_fields(self) -> dict[str, Field]: ... - - @cached_property - def compile_flag(self) -> ast.Assign: ... - - @property - def fields_assignment(self) -> ast.Assign: ... - - @property - def internals_assignment(self) -> ast.Assign: ... - - @property - def slots_assignment(self) -> ast.Assign: ... - - @property - def match_args_assignment(self) -> ast.Assign: ... - - @property - def init_method(self) -> ast.FunctionDef: ... - - @property - def repr_method(self) -> ast.FunctionDef: ... - - @property - def eq_method(self) -> ast.FunctionDef: ... - - @property - def iter_method(self) -> ast.FunctionDef: ... - - @property - def frozen_exception_import(self) -> ast.ImportFrom: ... - - @property - def frozen_setattr_method(self) -> ast.FunctionDef: ... - - @property - def frozen_delattr_method(self) -> ast.FunctionDef: ... - - def generate_ast(self, prefabs: dict[str, "PrefabDetails"]) -> None: ... - -class GatherClassAttrs(ast.NodeVisitor): - attrnames: Incomplete - func_arguments: Incomplete - def __init__(self) -> None: ... - def visit_FunctionDef(self, node: ast.FunctionDef): ... - def visit_Assign(self, node: ast.Assign): ... - def visit_AnnAssign(self, node: ast.AnnAssign): ... - -class GatherPrefabs(ast.NodeVisitor): - prefabs: Incomplete - dynamic_prefabs_present: bool - import_statements: Incomplete - def __init__(self) -> None: ... - def visit_ClassDef(self, node: ast.ClassDef) -> None: ... - def visit_ImportFrom(self, node: ast.ImportFrom) -> None: ... - -def compile_prefabs(source: str) -> ast.Module: ... diff --git a/stubs/prefab_classes/compiled/rewrite_source.pyi b/stubs/prefab_classes/compiled/rewrite_source.pyi deleted file mode 100644 index 4145c87..0000000 --- a/stubs/prefab_classes/compiled/rewrite_source.pyi +++ /dev/null @@ -1,12 +0,0 @@ -from ..exceptions import CompiledPrefabError as CompiledPrefabError - -COMPILE_COMMENT: str - -def rewrite_code(source: str, *, use_black: bool = ...): ... -def preview(pth, *, use_black: bool = ...): ... -def rewrite_to_py( - source_path, dest_path, *, header_comment: str = ..., use_black: bool = ... -): ... -def get_sources_to_compare( - source_path, dest_path, *, header_comment: str = ..., use_black: bool = ... -) -> tuple[str, str]: ... diff --git a/stubs/prefab_classes/constants.pyi b/stubs/prefab_classes/constants.pyi deleted file mode 100644 index 8f34ea3..0000000 --- a/stubs/prefab_classes/constants.pyi +++ /dev/null @@ -1,10 +0,0 @@ -PRE_INIT_FUNC: str -POST_INIT_FUNC: str -PREFAB_INIT_FUNC: str -DECORATOR_NAME: str -ATTRIBUTE_FUNCNAME: str -FIELDS_ATTRIBUTE: str -COMPILED_FLAG: str -COMPILE_ARGUMENT: str -CLASSVAR_NAME: str -INTERNAL_DICT: str diff --git a/stubs/prefab_classes/dynamic/__init__.pyi b/stubs/prefab_classes/dynamic/__init__.pyi deleted file mode 100644 index ae62da4..0000000 --- a/stubs/prefab_classes/dynamic/__init__.pyi +++ /dev/null @@ -1,5 +0,0 @@ -from .prefab import ( - attribute as attribute, - build_prefab as build_prefab, - prefab as prefab, -) diff --git a/stubs/prefab_classes/dynamic/_attribute_class.pyi b/stubs/prefab_classes/dynamic/_attribute_class.pyi deleted file mode 100644 index 6ab4cac..0000000 --- a/stubs/prefab_classes/dynamic/_attribute_class.pyi +++ /dev/null @@ -1,31 +0,0 @@ -from ..exceptions import LivePrefabError as LivePrefabError -from ..sentinels import NOTHING as NOTHING -from ..sentinels import _NOTHING_TYPE -import typing -from _collections_abc import Callable - -class Attribute: - COMPILED: bool - PREFAB_FIELDS: list[str] - __match_args__: tuple[str, ...] - default: typing.Any - default_factory: Callable[[], typing.Any] | _NOTHING_TYPE - init: bool - repr: bool - compare: bool - kw_only: bool - exclude_field: bool - _type: str | type | _NOTHING_TYPE - def __init__( - self, - *, - default=..., - default_factory=..., - init: bool = ..., - repr: bool = ..., - compare: bool = ..., - kw_only: bool = ..., - exclude_field: bool = ..., - ) -> None: ... - @staticmethod - def __prefab_pre_init__(init, default, default_factory, kw_only) -> None: ... diff --git a/stubs/prefab_classes/dynamic/autogen.pyi b/stubs/prefab_classes/dynamic/autogen.pyi deleted file mode 100644 index 1b90437..0000000 --- a/stubs/prefab_classes/dynamic/autogen.pyi +++ /dev/null @@ -1,7 +0,0 @@ -import typing -from collections.abc import Callable - -def autogen( - func: Callable[[type], str], - globs: dict[str, typing.Any] | None = ... -) -> type: ... diff --git a/stubs/prefab_classes/dynamic/method_generators.pyi b/stubs/prefab_classes/dynamic/method_generators.pyi deleted file mode 100644 index 7c2be60..0000000 --- a/stubs/prefab_classes/dynamic/method_generators.pyi +++ /dev/null @@ -1,25 +0,0 @@ -from ..constants import ( - FIELDS_ATTRIBUTE as FIELDS_ATTRIBUTE, - POST_INIT_FUNC as POST_INIT_FUNC, - PREFAB_INIT_FUNC as PREFAB_INIT_FUNC, - PRE_INIT_FUNC as PRE_INIT_FUNC, -) -from ..exceptions import FrozenPrefabError as FrozenPrefabError -from ..sentinels import NOTHING as NOTHING -from .autogen import autogen as autogen - -def get_init_maker(*, init_name: str = ...) -> type: ... -def get_repr_maker(will_eval: bool = ...) -> type: ... -def get_eq_maker() -> type: ... -def get_iter_maker() -> type: ... -def get_frozen_setattr_maker() -> type: ... -def get_frozen_delattr_maker() -> type: ... - -init_maker: type -prefab_init_maker: type -repr_maker: type -repr_maker_no_eval: type -eq_maker: type -iter_maker: type -frozen_setattr_maker: type -frozen_delattr_maker: type diff --git a/stubs/prefab_classes/dynamic/prefab.pyi b/stubs/prefab_classes/dynamic/prefab.pyi deleted file mode 100644 index 4beda14..0000000 --- a/stubs/prefab_classes/dynamic/prefab.pyi +++ /dev/null @@ -1,106 +0,0 @@ -from ..constants import ( - CLASSVAR_NAME as CLASSVAR_NAME, - COMPILED_FLAG as COMPILED_FLAG, - FIELDS_ATTRIBUTE as FIELDS_ATTRIBUTE, - POST_INIT_FUNC as POST_INIT_FUNC, - PRE_INIT_FUNC as PRE_INIT_FUNC, -) -from ..exceptions import ( - CompiledPrefabError as CompiledPrefabError, - LivePrefabError as LivePrefabError, - PrefabError as PrefabError, -) -from ..sentinels import KW_ONLY as KW_ONLY, NOTHING as NOTHING -from ._attribute_class import Attribute as Attribute -from .method_generators import ( - eq_maker as eq_maker, - frozen_delattr_maker as frozen_delattr_maker, - frozen_setattr_maker as frozen_setattr_maker, - init_maker as init_maker, - iter_maker as iter_maker, - prefab_init_maker as prefab_init_maker, - repr_maker as repr_maker, - repr_maker_no_eval as repr_maker_no_eval, -) - -from collections.abc import Callable -from typing import dataclass_transform, overload, TypeVar - -_T = TypeVar("_T") - -SLOW_TYPING: bool - -def attribute( - *, - default=..., - default_factory=..., - init: bool = ..., - repr: bool = ..., - compare: bool = ..., - kw_only: bool = ..., - exclude_field: bool = ..., -): ... - -@dataclass_transform(field_specifiers=(attribute,)) -def _make_prefab( - cls: type[_T], - *, - init: bool = ..., - repr: bool = ..., - eq: bool = ..., - iter: bool = ..., - match_args: bool = ..., - kw_only: bool = ..., - frozen: bool = ..., -) -> type[_T]: ... - - -@overload -def prefab( - cls: None = ..., - *, - init: bool = ..., - repr: bool = ..., - eq: bool = ..., - iter: bool = ..., - match_args: bool = ..., - kw_only: bool = ..., - frozen: bool = ..., - compile_prefab: bool = ..., - compile_fallback: bool = ..., - compile_plain: bool = ..., - compile_slots: bool = ..., -) -> Callable[[type[_T]], type[_T]]: ... - -@overload -# @dataclass_transform(field_specifiers=(attribute,)) -def prefab( - cls: type[_T], - *, - init: bool = ..., - repr: bool = ..., - eq: bool = ..., - iter: bool = ..., - match_args: bool = ..., - kw_only: bool = ..., - frozen: bool = ..., - compile_prefab: bool = ..., - compile_fallback: bool = ..., - compile_plain: bool = ..., - compile_slots: bool = ..., -) -> type[_T]: ... - -def build_prefab( - class_name: str, - attributes: list[tuple[str, Attribute]], - *, - bases: tuple[type, ...] = ..., - class_dict: None | dict[str, object] = ..., - init: bool = ..., - repr: bool = ..., - eq: bool = ..., - iter: bool = ..., - match_args: bool = ..., - kw_only: bool = ..., - frozen: bool = ..., -) -> type: ... diff --git a/stubs/prefab_classes/exceptions.pyi b/stubs/prefab_classes/exceptions.pyi deleted file mode 100644 index 307bdf7..0000000 --- a/stubs/prefab_classes/exceptions.pyi +++ /dev/null @@ -1,4 +0,0 @@ -class PrefabError(Exception): ... -class LivePrefabError(PrefabError): ... -class CompiledPrefabError(PrefabError): ... -class FrozenPrefabError(PrefabError): ... diff --git a/stubs/prefab_classes/funcs.pyi b/stubs/prefab_classes/funcs.pyi deleted file mode 100644 index 66fa0da..0000000 --- a/stubs/prefab_classes/funcs.pyi +++ /dev/null @@ -1,16 +0,0 @@ -from .constants import FIELDS_ATTRIBUTE as FIELDS_ATTRIBUTE -from collections.abc import Callable -import typing - -def is_prefab(o: typing.Any) -> bool: ... -def is_prefab_instance(o: typing.Any) -> bool: ... -def as_dict( - inst: object, *, excludes: typing.Union[None, tuple[str, ...]] = ... -) -> dict[str, object]: ... -def to_json( - inst: object, - *, - excludes: typing.Union[None, tuple[str, ...]] = ..., - dumps_func: typing.Union[None, Callable[..., str]] = ..., - **kwargs, -) -> str: ... diff --git a/stubs/prefab_classes/hook.pyi b/stubs/prefab_classes/hook.pyi deleted file mode 100644 index 6ddaa65..0000000 --- a/stubs/prefab_classes/hook.pyi +++ /dev/null @@ -1,25 +0,0 @@ -from _typeshed import Incomplete -from importlib.machinery import PathFinder, SourceFileLoader - -class PrefabHacker(SourceFileLoader): - def __getattribute__(self, item): ... - - # Pycharm and mypy incorrectly get source_to_code from InspectLoader instead of SourceLoader - # noinspection PyMethodOverriding - def source_to_code(self, data, path, *, _optimize: int = ...): ... # type: ignore - @staticmethod - def make_pyc_hash(source_bytes: bytes): ... - def get_code(self, fullname): ... - -class PrefabFinder(PathFinder): - @classmethod - def find_spec( - cls, fullname, path: Incomplete | None = ..., target: Incomplete | None = ... - ): ... - -def insert_prefab_importhook() -> None: ... -def remove_prefab_importhook() -> None: ... - -class prefab_compiler: - def __enter__(self) -> None: ... - def __exit__(self, exc_type, exc_val, exc_tb) -> None: ... diff --git a/stubs/prefab_classes/sentinels.pyi b/stubs/prefab_classes/sentinels.pyi deleted file mode 100644 index 5df9ca3..0000000 --- a/stubs/prefab_classes/sentinels.pyi +++ /dev/null @@ -1,7 +0,0 @@ -class _NOTHING_TYPE: ... - -NOTHING: _NOTHING_TYPE - -class _KW_ONLY_TYPE: ... - -KW_ONLY: _KW_ONLY_TYPE