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

Typing and export PresenceError #3

Merged
merged 10 commits into from
Jun 5, 2023
Merged

Typing and export PresenceError #3

merged 10 commits into from
Jun 5, 2023

Conversation

Amund211
Copy link
Contributor

Just upstreaming some more of the changes I've made in my copy of your library (Amund211/prism@358bd76), though a bit less invasive this time. Feel free to pick and choose which commits you want or don't want.

Summary of the changes:

  • Made explicit that Presence is exported from __init__.py (mypy was complaining about this)
  • Added some more type hints in presence.py to make it pass mypy --strict (there are still some Any types and 1 call to cast
  • Added a py.typed marker. This makes the type hints visible to people using the library, as before you would get an error that the module was installed, but could not be type-checked
  • Added PresenceError as an export from __init__.py
  • Fix type-checking on windows
  • Always do platform check the same way (ref last commit)

Previously type checkers like mypy would give an error when typechecking
code using the library: Skipping analyzing "discordrp": module is
installed, but missing library stubs or py.typed marker
This patch fixes this issue.
According to
https://peps.python.org/pep-0484/#version-and-platform-checking
type checkers should understand simple platform checks, however
`self._platform == WINDOWS` is not simple enough for mypy 1.3.0.
Replaced it with `sys.platform == 'win32'` so the type-checker doesn't
get mad at `socket.AF_UNIX` on windows.
This doesn't help the type-checking, but makes all the platform checks
done in the same way in an attempt to be less confusing.
@Amund211
Copy link
Contributor Author

For the sys.platform thing I tried keeping the variable WINDOWS so we don't have to duplicate the string literal everywhere, but mypy didn't like it.
Amund211/prism@70ebb79: https://github.com/Amund211/prism/actions/runs/5037578368/jobs/9034513970
Amund211/prism@d918bea: https://github.com/Amund211/prism/actions/runs/5037594511/jobs/9034540744

@TenType
Copy link
Owner

TenType commented May 22, 2023

Thanks for all the help. Right now I am occupied with work but I'll take a look at it when I get the chance.

Amund211 and others added 2 commits May 22, 2023 22:37
When no exception is raised these variables are None
Copy link
Owner

@TenType TenType left a comment

Choose a reason for hiding this comment

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

Sorry for the delay! At this point, I think it would be better if separate socket classes were made for both platforms to avoid leaving the socket typed as Any. I kept your commits but also added my own refactoring. From my end on MacOS, it's still mypy --strict compliant, but let me know if it doesn't work for you or if you have any suggestions.

Copy link
Owner

@TenType TenType left a comment

Choose a reason for hiding this comment

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

Prefer Optional[T] over Union[T, None]

discordrp/presence.py Outdated Show resolved Hide resolved
discordrp/presence.py Outdated Show resolved Hide resolved
discordrp/presence.py Outdated Show resolved Hide resolved
@Amund211
Copy link
Contributor Author

Amund211 commented Jun 4, 2023

Separate socket classes might be the cleanest option. We're still left with errors from mypy on windows, but that's just because the logic that chooses the _Socket implementation is way to advanced for mypy to understand. See: https://github.com/Amund211/discord-rich-presence/actions/runs/5171453202

Adding a # type: ignore just gives errors on the unicies because it's not a type error there lmao. # type: ignore [attr-defined,unused-ignore] should work in the next release of mypy, other than that I don't really know how to fix it. Then again, I don't think client code is affected by internal type errors in a library anyway, so it might be just okay to leave it, at least until the next mypy version. If that sounds OK to you you can cherry pick this: Amund211@4e6e497.

Also, when adding the action to test the typing on windows I realized that I broke python 3.9 compatibility with the new union syntax, so I changed it to the old one in 0a5623c (now 48f192f).

:^)

@Amund211
Copy link
Contributor Author

Amund211 commented Jun 4, 2023

Prefer Optional[T] over Union[T, None]

Fair, I'll just change the commit and force push

The library supports python 3.9, which does not support union with |
This fixes typechecking on windows, as socket.AF_UNIX is not defined
there. Adding unused-ignore also fixes the unused ignore error on mac
and linux.
@TenType
Copy link
Owner

TenType commented Jun 5, 2023

Thanks! I'll use # type: ignore [attr-defined,unused-ignore] for now then.

@TenType TenType merged commit f30085b into TenType:main Jun 5, 2023
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