Skip to content

chore: type annotations - convert uses of Optional and some Union usage to union operator | #2971

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

Merged
merged 3 commits into from
Nov 6, 2024

Conversation

edmondchuc
Copy link
Contributor

This PR is a follow-up to #2963. Largely, it updates usages of typing.Optional and typing.Union type annotations to the more modern and preferred union operator |.

Some other notes:

  • Some w3c sparql10 tests now passing, but not sure how. Results from the earl report has changed.

Summary of changes

Checklist

  • Checked that there aren't other open pull requests for
    the same change.
  • Checked that all tests and type checking passes.
  • If the change adds new features or changes the RDFLib public API:
    • Created an issue to discuss the change and get in-principle agreement.
    • Considered adding an example in ./examples.
  • If the change has a potential impact on users of this project:
    • Added or updated tests that fail without the change.
    • Updated relevant documentation to avoid inaccuracies.
    • Considered adding additional documentation.
  • Considered granting push permissions to the PR branch,
    so maintainers can fix minor issues and keep your PR up to date.

some w3c sparql10 tests now passing, but not sure how
@edmondchuc edmondchuc marked this pull request as draft November 5, 2024 02:49
@edmondchuc edmondchuc marked this pull request as ready for review November 5, 2024 13:24
@coveralls
Copy link

Coverage Status

coverage: 90.248% (-0.001%) from 90.249%
when pulling 1e36c39 on edmond/types
into 4ea72ac on main.

@ashleysommer
Copy link
Contributor

@edmondchuc
Did you need to fix any typing errors that arose from these changes and needed to be fixed? I expect if they are all simply replacing Union and Optional with their alternate forms, then MyPy should not have generated any new errors.

Something else to note, on the topic of Optional[X] vs X | None personally I prefer the former. Even though now the Python developers discourage its use and advocate to use the | None form, I personally tend to still use Optional[] in my projects, as a matter of personal taste. I think its because it reminds me of the Rust Option<> type.

@edmondchuc
Copy link
Contributor Author

I didn't run into any mypy errors. No runtime changes and no semantic changes to the codebase.

Something else to note, on the topic of Optional[X] vs X | None personally I prefer the former. Even though now the Python developers discourage its use and advocate to use the | None form, I personally tend to still use Optional[] in my projects, as a matter of personal taste. I think its because it reminds me of the Rust Option<> type.

Yeah, that's fair. I definitely was tossing up between the two, but the python docs helped me over the line given that their official stance is to prefer the union operator now.

For others reading this. From the python typing docs:

To define a union, use e.g. Union[int, str] or the shorthand int | str. Using that shorthand is recommended.

Keep in mind that Optional is shorthand for Union.

Optional[X] is equivalent to X | None (or Union[X, None]).

@edmondchuc edmondchuc changed the title chore: type annotations update chore: type annotations - convert uses of Optional and some Union usage to union operator | Nov 6, 2024
@edmondchuc edmondchuc merged commit ac0566f into main Nov 6, 2024
22 checks passed
@edmondchuc edmondchuc deleted the edmond/types branch November 6, 2024 03:25
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.

3 participants