Skip to content

Enable Ruff pyupgrade (UP) rule #365

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 1 commit into from
Aug 20, 2025

Conversation

mwtoews
Copy link
Contributor

@mwtoews mwtoews commented Aug 20, 2025

This enables Ruff's pyupgrade (UP) rule.

The associated fixes are automated (no manual edits) and are either UP007 or UP045.

@JamesParrott
Copy link
Collaborator

JamesParrott commented Aug 20, 2025

I'm not really a fan of this rule Mike, but this has exposed a gap in our testing, so thanks very much!

I don;t know why the Python 3.9 tests have passed when given Python 3.10 Type Expression syntax (| instead of Union).

Python 3.9.23 (main, Jun  4 2025, 17:36:12) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x: str | int
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'type'

So I don't think that syntax has been backported? Surely Ruff or Pyupgrade check the project metadata: requires-python = ">=3.9"?

@JamesParrott
Copy link
Collaborator

Oh I see, it's because of the future import.

Python 3.9.1 (default, Jan  3 2021, 22:36:18) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x: int | None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
>>> from __future__ import annotations
>>> x: int | None

@JamesParrott
Copy link
Collaborator

The tests do enforce Python 3.9 syntax: https://github.com/JamesParrott/IronPyShp/actions/runs/17095556570

@JamesParrott JamesParrott merged commit 86d7f04 into GeospatialPython:master Aug 20, 2025
104 checks passed
@JamesParrott
Copy link
Collaborator

Merged! I wasn't a fan of having hundreds of Unions and Optionals either. I'm warming to what PyUpgrade's trying to do.

@mwtoews
Copy link
Contributor Author

mwtoews commented Aug 20, 2025

I'm warming to what PyUpgrade's trying to do.

Likewise, I wasn't initially keen on these changes, but am seeing that it is a simpler mark-up in the long run. And correct, I forgot to mention that from __future__ import annotations allows | syntax for oldish Python versions.

@mwtoews mwtoews deleted the ruff-up branch August 20, 2025 20:52
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