Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More types #297

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

LiteApplication
Copy link
Contributor

Depends on #285 New Serializable (v3)

All the types described in the list on Wiki.vg are now implemented and fully tested.

@LiteApplication LiteApplication force-pushed the more-types branch 2 times, most recently from 9936baf to b5a9cd5 Compare May 17, 2024 15:02
Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21478 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21767 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21865 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21865 lines exceeds the maximum allowed for the inline comments feature.

@LiteApplication LiteApplication marked this pull request as ready for review June 8, 2024 21:41
Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 18380 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 18380 lines exceeds the maximum allowed for the inline comments feature.

Copy link
Member

@ItsDrike ItsDrike left a comment

Choose a reason for hiding this comment

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

Thanks for putting so much work into the lib! I really appreciate it.

I have managed to finally go through most of this and review the code, though I haven't yet looked into the entities and codegen script. As there were a fair few things in the other manually written types that I found and I didn't want to wait with submitting the review until I look over all of the entity stuff too.

Hopefully I'll manage to get through that some time soon too, but for now, here's your task list haha.

mcproto/types/chat.py Outdated Show resolved Hide resolved
mcproto/types/bitset.py Outdated Show resolved Hide resolved
mcproto/types/bitset.py Outdated Show resolved Hide resolved
mcproto/types/bitset.py Outdated Show resolved Hide resolved
mcproto/types/bitset.py Outdated Show resolved Hide resolved
mcproto/types/vec3.py Show resolved Hide resolved
mcproto/types/chat.py Show resolved Hide resolved
mcproto/types/chat.py Outdated Show resolved Hide resolved
mcproto/types/chat.py Outdated Show resolved Hide resolved
docs/api/types/index.rst Show resolved Hide resolved
@LiteApplication
Copy link
Contributor Author

Thank you for the review, I'm about to commit my changes but should I add the new types I wrote for the packets ?

The current PR contains everything listed on the top of the "Current protocol" page, but there are some types missing that I noticed and implemented later.

@ItsDrike
Copy link
Member

Oh, well, if the types are needed by some packets then yeah add them, if not, why are they even useful for us?

@LiteApplication
Copy link
Contributor Author

They are defined later in the page, or on other page, it's a bit of a mess (like the whole brigadier for example, or the Trade format, the crafting recipe type, ...)

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21015 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21048 lines exceeds the maximum allowed for the inline comments feature.

@LiteApplication
Copy link
Contributor Author

The PR diff size of 21048 lines exceeds the maximum allowed for the inline comments feature.

Don't worry buddy, it's gonna be alright.

@LiteApplication LiteApplication marked this pull request as draft June 18, 2024 16:48
@LiteApplication
Copy link
Contributor Author

Marking as draft to give me time to use NamedTuples when it's needed

Copy link
Member

@ItsDrike ItsDrike left a comment

Choose a reason for hiding this comment

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

Had some time, so I reviewed some more.

mcproto/packets/handshaking/handshake.py Show resolved Hide resolved
Comment on lines +147 to +142
:type uuid: :class:`~mcproto.types.UUID`
:param username: The username of the connecting player/client.
:type username: str
Copy link
Member

@ItsDrike ItsDrike Jun 18, 2024

Choose a reason for hiding this comment

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

The type should be picked up from the type-hint by sphinx autodoc, it shouldn't be necessary to doc it manually.

There's a lot of these in the code, this suggestion applies to all such instances.
(just use grep/rg to find all of them (or awk/tr like a chad) (or your editor's autoreplace, like a non-chad))

Suggested change
:type uuid: :class:`~mcproto.types.UUID`
:param username: The username of the connecting player/client.
:type username: str
:param username: The username of the connecting player/client.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They add the type between parenthesis (and links to the actual type) along with saying if it is optional :

Without :type ...: ...

Parameters:

  • item – The item ID of the item in the slot.
  • num – The count of items in the slot.
  • nbt – The NBT data of the item in the slot, None if there is no item or no NBT data.

With it :

Parameters:

  • item (int) – The item ID of the item in the slot.
  • num (int) – The count of items in the slot.
  • nbt (NBTag, optional) – The NBT data of the item in the slot, None if there is no item or no NBT data.

Also these type informations only appear on NamedTuples (without the helpful description next to them), and don't appear at all for regular Serializables.

Copy link
Member

Choose a reason for hiding this comment

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

hmm, I think this might be configurable with autosphinx, will need to check though

mcproto/types/advancement.py Outdated Show resolved Hide resolved
mcproto/types/advancement.py Outdated Show resolved Hide resolved
mcproto/types/bitset.py Outdated Show resolved Hide resolved
mcproto/types/nbt.py Outdated Show resolved Hide resolved
mcproto/types/nbt.py Outdated Show resolved Hide resolved
mcproto/types/recipe.py Outdated Show resolved Hide resolved
mcproto/utils/abc.py Show resolved Hide resolved
tests/mcproto/types/test_identifier.py Show resolved Hide resolved
@LiteApplication
Copy link
Contributor Author

LiteApplication commented Jun 18, 2024

Thing left to do before merging :

  • Use attrs validators instead of the validate function
  • Use converters when applicable
  • Get some actual data on the NBT serialization process

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21180 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21185 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21194 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21194 lines exceeds the maximum allowed for the inline comments feature.

@ItsDrike ItsDrike added p: 1 - high This should be addressed quickly t: feature New request or feature a: protocol Related to underlying networking protocol (connections, buffers, readers/writers, type classes) a: API Related to exposed core API of the project labels Jun 29, 2024
@LiteApplication LiteApplication marked this pull request as ready for review July 8, 2024 09:10
@LiteApplication
Copy link
Contributor Author

I didn't manage to check the NBT for myself in this particular case but I was able to extract some packets and verify that they do not include the name tag, as stated in the documentation.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21194 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21250 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21250 lines exceeds the maximum allowed for the inline comments feature.

Copy link

@codeclimate codeclimate bot left a comment

Choose a reason for hiding this comment

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

The PR diff size of 21246 lines exceeds the maximum allowed for the inline comments feature.

@LiteApplication
Copy link
Contributor Author

I re-ran the tests with basedpyright, everything seems fine

@LiteApplication
Copy link
Contributor Author

Shouldn't this get merged ?

 * Angle
 * Bitset and FixedBitset
 * Position
 * Vec3
 * Quaternion
 * Slot
 * Identifier
 * TextComponent
- Rename ChatMessage to JSONTextComponent
* Modified the conf.py sphinx configuration file to ignore the custom fields used in the Entity Metadata type
* Added the Entity Metadata type to the documentation
* Added `scripts` to the .codeclimate.yml ignore list
* Added a changelog entry for the pull request
- Added sphinx build for the types
- Use None as the default nbt value for Slot
- Add even more types and tests
- Cache the classes for FixedBitset
- Create an `AdvancementFrame` IntEnum
- Use attrs' validators and convertors
when validating a single variable

Fix some linting problems
- Update particle IDs in Particle Data (we really need that Registry stuff)
- Add tests for ParticleData
- Split Masked proxy EME into BoolMasked and IntMasked for improved speed, simplicity and type checking
Source : Conventions

Fix one minor `basedpyright` issue
Copy link
Member

@ItsDrike ItsDrike left a comment

Choose a reason for hiding this comment

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

Sorry the review took this long, I was pretty busy with other things recently, but mostly scared/too lazy to look at this much code lol. I've now gone over all of the files in the PR (finally), so these changes should be the last requested changes that need addressing, the rest looks good.

Also, a rebase with main should fix the CI failures.

Comment on lines +17 to +18
_NAMESPACE_REGEX = re.compile(r"^[a-z0-9\.\-_]+$")
_PATH_REGEX = re.compile(r"^[a-z0-9\.\-_/]+$")
Copy link
Member

Choose a reason for hiding this comment

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

Even though attrs won't consider these as instance variables, because there isn't a type-hint specified, the recommended practice for class vars is to annotate them with typing.ClassVar, you don't have to specify the generic type here, pyright can infer that, but do add the : ClassVar hint here.

Suggested change
_NAMESPACE_REGEX = re.compile(r"^[a-z0-9\.\-_]+$")
_PATH_REGEX = re.compile(r"^[a-z0-9\.\-_/]+$")
_NAMESPACE_REGEX: ClassVar = re.compile(r"^[a-z0-9\.\-_]+$")
_PATH_REGEX: ClassVar = re.compile(r"^[a-z0-9\.\-_/]+$")

@@ -3,10 +3,10 @@
from abc import abstractmethod
from collections.abc import Iterator, Mapping, Sequence
from enum import IntEnum
from typing import ClassVar, Protocol, Union, cast, final, runtime_checkable
from typing import ClassVar, Hashable, Protocol, Union, cast, final, runtime_checkable
Copy link
Member

Choose a reason for hiding this comment

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

Huh, I didn't even know typing was re-exporting collections.abc.Hashable, it's not a generic type and it is available in 3.8, so there's no reason not to use the proper collections.abc version here.

def read_header(cls, buf: Buffer, return_type: Literal[False] = False) -> int: ...

@classmethod
def read_header(cls, buf: Buffer, return_type: bool = False) -> tuple[int, int] | int:
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this function be private? Same thing for read_value.

Comment on lines +23 to +31
@staticmethod
def finite_validator(instance: Vec3, attribute: Attribute[float], value: float) -> None:
"""Validate that the quaternion components are finite."""
if not math.isfinite(value):
raise ValueError(f"Quaternion components must be finite, got {value!r}")

x: float = field(validator=[validators.instance_of((float, int)), finite_validator.__get__(object)])
y: float = field(validator=[validators.instance_of((float, int)), finite_validator.__get__(object)])
z: float = field(validator=[validators.instance_of((float, int)), finite_validator.__get__(object)])
Copy link
Member

Choose a reason for hiding this comment

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

What's the point of that finite_validator.__get__(object)? From my quick testing, it seems that you can just use finite_validator on it's own and it will work just fine.

Same remark for the Quaternion class which also does this, and any other occurences of this which I might've missed.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it is a static method, not a regular function, in python 3.8 the __get__ part is needed to get a function because we are getting it from inside the class at creation time.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I figured it's to get the function, I didn't realize it didn't work on 3.8 though, okay then but do add a comment explaining this in all occurrences of doing this.

Alternatively, I wouldn't even mind moving the functions outside of the classes.

Copy link
Member

Choose a reason for hiding this comment

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

Rename this file to _entity_generator_data.py, just to better distinguish it from runnable scripts.

Comment on lines +91 to +96
def to_vec3(self) -> Vec3:
"""Convert the vector to a Vec3.

This function creates a new Vec3 object with the same components.
"""
return Vec3(x=self.x, y=self.y, z=self.z)
Copy link
Member

Choose a reason for hiding this comment

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

Add some note to the docstring here that this function is primarily intended to be used by subclasses of Vec3 / Position class. It just looks really weird to see Vec3.to_vec3 function.

@LiteApplication
Copy link
Contributor Author

Ok great, I have been a little busy too. I'll try to make the changes soon !

@py-mine-ci-bot py-mine-ci-bot bot added the s: stale Has had no activity for a while (will be closed for inactivity/marked up for grabs soon) label Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: API Related to exposed core API of the project a: protocol Related to underlying networking protocol (connections, buffers, readers/writers, type classes) p: 1 - high This should be addressed quickly s: stale Has had no activity for a while (will be closed for inactivity/marked up for grabs soon) t: feature New request or feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants