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

Type annotate the ndspy.bmg module #10

Merged

Conversation

mike8699
Copy link
Contributor

@mike8699 mike8699 commented Sep 11, 2023

No description provided.

Copy link
Owner

@RoadrunnerWMC RoadrunnerWMC left a comment

Choose a reason for hiding this comment

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

Apologies for the delay in looking at this.

This PR seems to include everything from #9 along with the additional annotations, so I'll go ahead and close that in favor of this one.

@mike8699 mike8699 force-pushed the finish-bmg-type-annotations branch from d596179 to 8406e7a Compare September 21, 2023 01:10
Copy link
Owner

@RoadrunnerWMC RoadrunnerWMC left a comment

Choose a reason for hiding this comment

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

Just a couple more things I noticed after looking over it again, and then I think we'll be good to go!

ndspy/bmg.py Outdated

def __init__(self, data=None, *, id=0):
id: int
encoding: str
Copy link
Owner

Choose a reason for hiding this comment

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

Hm, can we make this a Literal too? Valid values for it are listed in the _ENCODINGS constant (ignore the None, that's just a placeholder because encoding values start at 1)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Sadly it's not possible to pass a list of strings to typing.Literal, so the string literals have to be duplicated between the Literal annotation and the _ENCODINGS list. I believe we could do it the other way around, i.e. something like this

import typing

Encoding = typing.Literal['cp1252', 'utf-16', 'shift-jis', 'utf-8']
_ENCODINGS = typing.get_args(Encoding)

But that would break 3.7 compatibility. I don't think it's that big of a deal to have them duplicated, but let me know if you'd prefer it be done a different way. (Plus, if mypy/some other type checker is eventually introduced as a CI check, it would catch any accidental drift between the type annotation and the _ENCODINGS list)

Copy link
Owner

Choose a reason for hiding this comment

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

I think it's good enough to just add a couple comments to indicate that the two should be kept in sync. I can do that after merging the PR.

ndspy/bmg.py Outdated
stringParts = None
isNull = False
info: bytes
stringParts: list[str]
Copy link
Owner

Choose a reason for hiding this comment

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

I almost forgot, this is actually a list[str | Escape], not just of str (this also applies to the corresponding __init__ argument)

@mike8699 mike8699 changed the title Type annotate the rest of the ndspy.bmg module Type annotate the ndspy.bmg module Sep 22, 2023
@RoadrunnerWMC RoadrunnerWMC merged commit f5a2d48 into RoadrunnerWMC:master Sep 22, 2023
@RoadrunnerWMC
Copy link
Owner

Thanks again!

@mike8699 mike8699 deleted the finish-bmg-type-annotations branch September 22, 2023 22:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants