feat: add Identifier type for Python bindings#373
Merged
Conversation
Replace transparent str conversion with a proper Identifier Python object that exposes namespace and key properties. The type caster accepts both str and Identifier inputs and returns Identifier objects.
Change PyRegistry::get, getOrThrow, and contains to accept PyIdentifier instead of raw std::string. Update Registry stubs to accept Identifier[_T] | str.
Without this, passing str to PyRegistry methods (get, contains, etc.) would raise TypeError since pybind11 doesn't auto-convert.
cc9e725 to
c351a9b
Compare
- Use pybind11's io_name so Identifier<T> casts to endstone.Identifier[T] on return but accepts endstone.Identifier[T] | str on input, reflecting the runtime behaviour without forcing every call site through the Identifier constructor. - Mirror __str__ in __repr__ on PyIdentifier. Users never construct Identifier directly (it's a facade for typed ids surfaced by the registry API), so the eval(repr(x)) convention buys nothing and the shorter form reads better in logs. - Add __eq__/__hash__/string-comparison overloads on BlockType and the missing __hash__ on ItemType so all four registry types (ActorType, BlockType, Enchantment, ItemType) are consistently hashable and string-comparable.
- Declare endstone-stubgen (from the EndstoneMC/stubgen GitHub source, which handles std::function callable annotations and equality overloads cleanly) and ruff as inline PEP 723 dependencies so `uv run scripts/stubgen.py` resolves them automatically. - Strip the bogus `from . import endstone` that Pybind11ImportFix injects alongside the absolute import. - Retype `NAME = Identifier(...)` class constants to `NAME: Identifier[<owner>] = "<key>"`, tracking the enclosing class by indentation so the generic type parameter survives and the bare string remains visible as documentation. - Rewrite `endstone.Identifier` references in submodules to bare `Identifier` plus a `from endstone import Identifier` so the constant blocks read cleanly. - Deduplicate exports so Identifier/Registry don't appear twice in `__all__`.
- Class constants like Dimension.OVERWORLD, ActorType.ZOMBIE, Enchantment.PROTECTION are now typed as `Identifier[Owner]` rather than plain `str`. Static type checkers reject passing `Dimension.OVERWORLD` where `Identifier[ActorType]` is expected. - Registry lookups and `Identifier<T>`-typed parameters accept either an Identifier or a str at runtime; stubs show this as `Identifier[T] | str` via the io_name split. - Expose Identifier, Registry, and BlockType through the lazy_loader forwarding in the relevant package __init__.py files so the runtime matches the stubs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Identifier<T>is used throughout the API (dimensions, block types, actor types, enchantments, etc.) but was exposed to Python as a plainstr. This had two problems:"namespace:key"strings to extract components.DimensionIdand anActorTypeIdsince both were juststr. You could passDimension.OVERWORLDtospawn_actor(type=...)without any type error.With
Identifier[T], type checkers can now catch misuse:Before
After
Summary
PyIdentifierstruct to exposeIdentifier<T>as a proper Python object withnamespaceandkeypropertiesIdentifier<T>type caster to returnIdentifierobjects instead ofstr, while still accepting bothstrandIdentifieras input__str__methods onBlockType,Enchantment, andItemTypeto returnstr(notIdentifier)Identifiergeneric facade in stubs and stubgen forIdentifier[T]type hintsPyIdentifierconstructorsTest plan
pytest tests/endstone_test)Dimension.OVERWORLD.namespace == "minecraft"and.key == "overworld"Dimension.OVERWORLD == "minecraft:overworld"backward compat