Skip to content

Conversation

@dalonsoa
Copy link
Collaborator

Description

pyupgrade is now enabled in ruff, using python 3.9 as the target since we want to keep the compatibility with it. As usual with these things, a lot of files are changed, but otherwise you should only worried about pyproject.toml.

Fixes #293

Type of change

Please add a line in the relevant section of
CHANGELOG.md to
document the change (include PR #) - note reverse order of PR #s.

  • New feature (non-breaking change which adds functionality)
  • Optimization (non-breaking, back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (whatever its nature)

Key checklist

  • All tests pass: $ python -m pytest
  • The documentation builds and looks OK: $ python -m sphinx -b html docs docs/build

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@dalonsoa dalonsoa requested review from alexdewar and tsmbland June 20, 2024 13:07
@codecov
Copy link

codecov bot commented Jun 20, 2024

Codecov Report

Attention: Patch coverage is 74.26710% with 79 lines in your changes missing coverage. Please review.

Project coverage is 71.24%. Comparing base (9938ba9) to head (e2e6888).

Files Patch % Lines
src/muse/readers/csv.py 40.47% 18 Missing and 7 partials ⚠️
src/muse/outputs/mca.py 42.10% 7 Missing and 4 partials ⚠️
src/muse/regressions.py 47.05% 9 Missing ⚠️
src/muse/investments.py 40.00% 1 Missing and 5 partials ⚠️
src/muse/examples.py 80.00% 3 Missing ⚠️
src/muse/production.py 57.14% 1 Missing and 2 partials ⚠️
src/muse/agents/factories.py 71.42% 0 Missing and 2 partials ⚠️
src/muse/hooks.py 75.00% 0 Missing and 2 partials ⚠️
src/muse/interactions.py 71.42% 0 Missing and 2 partials ⚠️
src/muse/objectives.py 75.00% 1 Missing and 1 partial ⚠️
... and 11 more
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #356      +/-   ##
===========================================
+ Coverage    71.14%   71.24%   +0.09%     
===========================================
  Files           44       44              
  Lines         5844     5870      +26     
  Branches      1158     1158              
===========================================
+ Hits          4158     4182      +24     
- Misses        1364     1366       +2     
  Partials       322      322              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@alexdewar alexdewar left a comment

Choose a reason for hiding this comment

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

I've skimmed this and it mostly looks totally fine (and the newer syntax is much nicer to look at!).

I am a little concerned that there are some places where you're writing union types as type_a | type_b, which is only available in Python 3.10. Oddly, none of our workflows have caught this (maybe because we don't have explicit tests for the type hints?), but I'm a bit concerned things will break at runtime for Python 3.9 users.

"outputs": [],
"source": [
"from typing import List, Optional, Text\n",
"from typing import Optional\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we have to wait for a newer version of Python until we can write Optionals as my_type | None!

market: xr.Dataset,
technologies: xr.Dataset,
year: Optional[int] = None,
year: int | None = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think writing union types this way is a Python 3.10 thing, alas: https://docs.python.org/3/library/typing.html#typing.Union

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's a bit weird that the Python 3.9 runners don't seem to be complaining... Maybe these bits aren't covered by tests?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These sort of things are checked by mypy which we are not running in MUSE because it largely lacks of consistent typing information. Maybe at runtime these things do not matter at all?

What annoys me is that I set as target python version 3.9, so if this is a 3.10 feature, it should not have been changed :(

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ugh, that is annoying. I guess it's a ruff bug? 😞

Strangely, there are plenty of other places where it's left Optional[int]s alone.

I suppose one option would be to do this first upgrade with pyupgrade itself, which presumably doesn't have this problem? I guess the ruff hook might then change these bits again, though. Perhaps if it's being really stubborn we should just use the pyupgrade pre-commit hook.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've just found the trick in here. Esentially, you can use | with no problem from python 3.7 as long as you do from __future__ import annotations which we do in all the files that ruff changed.

In summary, these changes are safe to go.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In the file where that was not changed it was because we did not have that import statement.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ohhh I see. Well that's a relief

@alexdewar alexdewar self-requested a review June 20, 2024 15:29
Copy link
Collaborator

@alexdewar alexdewar left a comment

Choose a reason for hiding this comment

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

LGTM!

@dalonsoa dalonsoa merged commit 130c8f1 into develop Jun 20, 2024
@dalonsoa dalonsoa deleted the pyupgrade branch June 20, 2024 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

ruff: Enable pyupgrade checks

4 participants