From 91eed3913ae3f17e4f0efb055ebf439740eedca1 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 19 Mar 2024 14:38:05 +0000 Subject: [PATCH 1/4] Fix type hints --- src/amulet_nbt/__init__.pyi | 10 +++++----- src/amulet_nbt/_tag/compound.pyx | 16 ++++++++-------- src/amulet_nbt/_tag/compound.pyx.tp | 16 ++++++++-------- src/amulet_nbt/_tag/list.pyx | 2 +- src/amulet_nbt/_tag/list.pyx.tp | 2 +- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/amulet_nbt/__init__.pyi b/src/amulet_nbt/__init__.pyi index a072ef1a..ea700200 100644 --- a/src/amulet_nbt/__init__.pyi +++ b/src/amulet_nbt/__init__.pyi @@ -334,7 +334,7 @@ class StringTag(AbstractBaseImmutableTag): def __lt__(self, other: Any) -> bool: """Check if the tag is less than another tag.""" -AnyNBTT = TypeVar("AnyNBTT", bound=AnyNBT) +AnyNBTT = TypeVar("AnyNBTT", bound=AbstractBaseTag) class ListTag(AbstractBaseMutableTag, MutableSequence[AnyNBTT]): @overload @@ -398,7 +398,7 @@ class ListTag(AbstractBaseMutableTag, MutableSequence[AnyNBTT]): @overload def __setitem__(self, index: int, value: AnyNBT) -> None: ... @overload - def __setitem__(self, index: slice, value: Iterable[AnyNBT]) -> None: ... + def __setitem__(self, index: slice, value: Iterable[AbstractBaseTag]) -> None: ... @overload def __delitem__(self, index: int) -> None: ... @overload @@ -408,11 +408,11 @@ class ListTag(AbstractBaseMutableTag, MutableSequence[AnyNBTT]): _TagT = TypeVar("_TagT", bound=AbstractBaseTag) -class CompoundTag(AbstractBaseMutableTag, MutableMapping[str | bytes, AnyNBT]): +class CompoundTag(AbstractBaseMutableTag, MutableMapping[str | bytes, AbstractBaseTag]): def __init__( self, - value: Mapping[str | bytes, AnyNBT] | Iterable[tuple[str | bytes, AnyNBT]] = (), - **kwvals: AnyNBT, + value: Mapping[str | bytes, AbstractBaseTag] | Iterable[tuple[str | bytes, AbstractBaseTag]] = (), + **kwvals: AbstractBaseTag, ): ... @property def py_dict(self) -> dict[str, AnyNBT]: diff --git a/src/amulet_nbt/_tag/compound.pyx b/src/amulet_nbt/_tag/compound.pyx index 46208d07..d68720c4 100644 --- a/src/amulet_nbt/_tag/compound.pyx +++ b/src/amulet_nbt/_tag/compound.pyx @@ -210,7 +210,7 @@ cdef AbstractBaseTag wrap_node(TagNode* node): raise RuntimeError -TagT = TypeVar("TagT", bound=AbstractBaseTag) +_TagT = TypeVar("_TagT", bound=AbstractBaseTag) cdef class CompoundTag(AbstractBaseMutableTag): @@ -222,8 +222,8 @@ cdef class CompoundTag(AbstractBaseMutableTag): def __init__( self, - value: Mapping[str | bytes, amulet_nbt.AnyNBT] | Iterable[tuple[str | bytes, amulet_nbt.AnyNBT]] = (), - **kwargs: amulet_nbt.AnyNBT, + value: Mapping[str | bytes, AbstractBaseTag] | Iterable[tuple[str | bytes, AbstractBaseTag]] = (), + **kwargs: AbstractBaseTag, ) -> None: self.cpp = make_shared[CCompoundTag]() self.update(value, **kwargs) @@ -315,9 +315,9 @@ cdef class CompoundTag(AbstractBaseMutableTag): return wrap_node(&dereference(it).second) @overload - def get(self, key: str | bytes, default: None = None) -> AnyNBT | None:... + def get(self, key: str | bytes, default: None = None) -> amulet_nbt.AnyNBT | None:... @overload - def get(self, key: str | bytes, default: _TagT = None) -> AnyNBT: ... + def get(self, key: str | bytes, default: _TagT = None) -> amulet_nbt.AnyNBT: ... @overload def get(self, key: str | bytes, default: None = None, cls: Type[_TagT] = AbstractBaseTag) -> _TagT | None: ... @overload @@ -326,9 +326,9 @@ cdef class CompoundTag(AbstractBaseMutableTag): def get( self, string key: str | bytes, - object default: TagT | None = None, - object cls: Type[TagT] = AbstractBaseTag - ) -> TagT | None: + object default: _TagT | None = None, + object cls: Type[_TagT] = AbstractBaseTag + ) -> _TagT | None: """Get an item from the CompoundTag. :param key: The key to get diff --git a/src/amulet_nbt/_tag/compound.pyx.tp b/src/amulet_nbt/_tag/compound.pyx.tp index 397336e6..a275e6bc 100644 --- a/src/amulet_nbt/_tag/compound.pyx.tp +++ b/src/amulet_nbt/_tag/compound.pyx.tp @@ -129,7 +129,7 @@ f""" {"el"*(index!=1)}if index == {index}: raise RuntimeError -TagT = TypeVar("TagT", bound=AbstractBaseTag) +_TagT = TypeVar("_TagT", bound=AbstractBaseTag) cdef class CompoundTag(AbstractBaseMutableTag): @@ -141,8 +141,8 @@ cdef class CompoundTag(AbstractBaseMutableTag): def __init__( self, - value: Mapping[str | bytes, amulet_nbt.AnyNBT] | Iterable[tuple[str | bytes, amulet_nbt.AnyNBT]] = (), - **kwargs: amulet_nbt.AnyNBT, + value: Mapping[str | bytes, AbstractBaseTag] | Iterable[tuple[str | bytes, AbstractBaseTag]] = (), + **kwargs: AbstractBaseTag, ) -> None: self.cpp = make_shared[CCompoundTag]() self.update(value, **kwargs) @@ -221,9 +221,9 @@ cdef class CompoundTag(AbstractBaseMutableTag): return wrap_node(&dereference(it).second) @overload - def get(self, key: str | bytes, default: None = None) -> AnyNBT | None:... + def get(self, key: str | bytes, default: None = None) -> amulet_nbt.AnyNBT | None:... @overload - def get(self, key: str | bytes, default: _TagT = None) -> AnyNBT: ... + def get(self, key: str | bytes, default: _TagT = None) -> amulet_nbt.AnyNBT: ... @overload def get(self, key: str | bytes, default: None = None, cls: Type[_TagT] = AbstractBaseTag) -> _TagT | None: ... @overload @@ -232,9 +232,9 @@ cdef class CompoundTag(AbstractBaseMutableTag): def get( self, string key: str | bytes, - object default: TagT | None = None, - object cls: Type[TagT] = AbstractBaseTag - ) -> TagT | None: + object default: _TagT | None = None, + object cls: Type[_TagT] = AbstractBaseTag + ) -> _TagT | None: """Get an item from the CompoundTag. :param key: The key to get diff --git a/src/amulet_nbt/_tag/list.pyx b/src/amulet_nbt/_tag/list.pyx index e2d32a43..5fdaeaba 100644 --- a/src/amulet_nbt/_tag/list.pyx +++ b/src/amulet_nbt/_tag/list.pyx @@ -2494,7 +2494,7 @@ cdef class ListTag(AbstractBaseMutableTag): """ tag_id: int = 9 - def __init__(self, object value: Iterable[amulet_nbt.AnyNBT] = (), char element_tag_id = 1) -> None: + def __init__(self, object value: Iterable[AbstractBaseTag] = (), char element_tag_id = 1) -> None: self.cpp = make_shared[CListTag]() if element_tag_id == 0: dereference(self.cpp).emplace[monostate]() diff --git a/src/amulet_nbt/_tag/list.pyx.tp b/src/amulet_nbt/_tag/list.pyx.tp index c3ed2189..976be479 100644 --- a/src/amulet_nbt/_tag/list.pyx.tp +++ b/src/amulet_nbt/_tag/list.pyx.tp @@ -493,7 +493,7 @@ cdef class ListTag(AbstractBaseMutableTag): """ tag_id: int = 9 - def __init__(self, object value: Iterable[amulet_nbt.AnyNBT] = (), char element_tag_id = 1) -> None: + def __init__(self, object value: Iterable[AbstractBaseTag] = (), char element_tag_id = 1) -> None: self.cpp = make_shared[CListTag]() if element_tag_id == 0: dereference(self.cpp).emplace[monostate]() From 8566485198a3cea1ee555891714321fe220936e0 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 19 Mar 2024 14:42:07 +0000 Subject: [PATCH 2/4] Converted init files to cython PyCharm was preferring these over the stub files making it difficult to debug. Hopefully renaming these will make it go straight to the stub files. --- src/amulet_nbt/{__init__.py => __init__.pyx} | 2 +- src/amulet_nbt/{_errors.py => _errors.pyx} | 0 .../_nbt_encoding/{__init__.py => __init__.pyx} | 0 src/amulet_nbt/_string_encoding/__init__.py | 6 ------ src/amulet_nbt/_string_encoding/__init__.pyx | 8 ++++++++ src/amulet_nbt/_tag/{__init__.py => __init__.pyx} | 0 6 files changed, 9 insertions(+), 7 deletions(-) rename src/amulet_nbt/{__init__.py => __init__.pyx} (98%) rename src/amulet_nbt/{_errors.py => _errors.pyx} (100%) rename src/amulet_nbt/_nbt_encoding/{__init__.py => __init__.pyx} (100%) delete mode 100644 src/amulet_nbt/_string_encoding/__init__.py create mode 100644 src/amulet_nbt/_string_encoding/__init__.pyx rename src/amulet_nbt/_tag/{__init__.py => __init__.pyx} (100%) diff --git a/src/amulet_nbt/__init__.py b/src/amulet_nbt/__init__.pyx similarity index 98% rename from src/amulet_nbt/__init__.py rename to src/amulet_nbt/__init__.pyx index fb49c904..9d44e0f5 100644 --- a/src/amulet_nbt/__init__.py +++ b/src/amulet_nbt/__init__.pyx @@ -118,7 +118,7 @@ from ._errors import NBTError, NBTLoadError, NBTFormatError, SNBTParseError -from ._string_encoding import ( +from ._string_encoding.encoding import ( mutf8_encoding, utf8_encoding, utf8_escape_encoding, diff --git a/src/amulet_nbt/_errors.py b/src/amulet_nbt/_errors.pyx similarity index 100% rename from src/amulet_nbt/_errors.py rename to src/amulet_nbt/_errors.pyx diff --git a/src/amulet_nbt/_nbt_encoding/__init__.py b/src/amulet_nbt/_nbt_encoding/__init__.pyx similarity index 100% rename from src/amulet_nbt/_nbt_encoding/__init__.py rename to src/amulet_nbt/_nbt_encoding/__init__.pyx diff --git a/src/amulet_nbt/_string_encoding/__init__.py b/src/amulet_nbt/_string_encoding/__init__.py deleted file mode 100644 index 98365d7a..00000000 --- a/src/amulet_nbt/_string_encoding/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -from .encoding import ( - mutf8_encoding, - utf8_encoding, - utf8_escape_encoding, - StringEncoding, -) diff --git a/src/amulet_nbt/_string_encoding/__init__.pyx b/src/amulet_nbt/_string_encoding/__init__.pyx new file mode 100644 index 00000000..8f67264f --- /dev/null +++ b/src/amulet_nbt/_string_encoding/__init__.pyx @@ -0,0 +1,8 @@ +# distutils: language = c++ +# distutils: extra_compile_args = CPPCARGS + +from amulet_nbt._string_encoding.encoding import ( + mutf8_encoding, + utf8_encoding, + utf8_escape_encoding +) diff --git a/src/amulet_nbt/_tag/__init__.py b/src/amulet_nbt/_tag/__init__.pyx similarity index 100% rename from src/amulet_nbt/_tag/__init__.py rename to src/amulet_nbt/_tag/__init__.pyx From b97ba61cfd1298170e3f99d4114cd8c5c316ba44 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 19 Mar 2024 15:37:11 +0000 Subject: [PATCH 3/4] Fixed package discovery --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index d76d09c3..0bd2a610 100644 --- a/setup.cfg +++ b/setup.cfg @@ -15,7 +15,7 @@ platforms = any [options] package_dir= =src -packages = find: +packages = find_namespace: zip_safe = False python_requires = ~=3.11 install_requires = From 2b6294261d6449b05536fe5e6e4d18080ac39dd0 Mon Sep 17 00:00:00 2001 From: gentlegiantJGC Date: Tue, 19 Mar 2024 15:43:29 +0000 Subject: [PATCH 4/4] Reformatted --- src/amulet_nbt/__init__.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/amulet_nbt/__init__.pyi b/src/amulet_nbt/__init__.pyi index ea700200..3d06653b 100644 --- a/src/amulet_nbt/__init__.pyi +++ b/src/amulet_nbt/__init__.pyi @@ -411,7 +411,10 @@ _TagT = TypeVar("_TagT", bound=AbstractBaseTag) class CompoundTag(AbstractBaseMutableTag, MutableMapping[str | bytes, AbstractBaseTag]): def __init__( self, - value: Mapping[str | bytes, AbstractBaseTag] | Iterable[tuple[str | bytes, AbstractBaseTag]] = (), + value: ( + Mapping[str | bytes, AbstractBaseTag] + | Iterable[tuple[str | bytes, AbstractBaseTag]] + ) = (), **kwvals: AbstractBaseTag, ): ... @property