feat: add Identifier type for Python bindings#373
Draft
wu-vincent wants to merge 3 commits intomainfrom
Draft
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.
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