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

mypy: Use "|" operator instead of Union/Optional #1345

Merged
merged 1 commit into from
Jun 4, 2022

Conversation

rht
Copy link
Contributor

@rht rht commented May 26, 2022

Partially addresses #1341. To completely remove Union, sadly, we need to wait until 3.10 is the oldest supported version.

See https://peps.python.org/pep-0604/.

@codecov
Copy link

codecov bot commented May 26, 2022

Codecov Report

Merging #1345 (03419e1) into main (991a017) will increase coverage by 0.16%.
The diff coverage is 100.00%.

❗ Current head 03419e1 differs from pull request most recent head 38d662e. Consider uploading reports for the commit 38d662e to get more accurate results

@@            Coverage Diff             @@
##             main    #1345      +/-   ##
==========================================
+ Coverage   90.97%   91.13%   +0.16%     
==========================================
  Files          19       19              
  Lines        1329     1342      +13     
  Branches      256      257       +1     
==========================================
+ Hits         1209     1223      +14     
+ Misses         85       84       -1     
  Partials       35       35              
Impacted Files Coverage Δ
mesa/agent.py 77.77% <100.00%> (+1.30%) ⬆️
mesa/model.py 100.00% <100.00%> (ø)
mesa/space.py 94.72% <100.00%> (+0.27%) ⬆️
mesa/time.py 94.79% <100.00%> (+0.05%) ⬆️
mesa/visualization/UserParam.py 95.00% <0.00%> (+0.38%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 991a017...38d662e. Read the comment docs.

@rht
Copy link
Contributor Author

rht commented May 26, 2022

@wang-boyu CI failed here. How did you make it work?

@wang-boyu
Copy link
Member

I'm guessing it's due to the type aliases TimeT = float | int, GridContent = Agent | None etc.

From here it mentions

Even with the future import, there are some scenarios that could still require string literals or result in errors, typically involving use of forward references or generics in:
type aliases;
...

A similar issue was reported: python/mypy#9610

@Corvince
Copy link
Contributor

TimeT: TypeAlias = float | int potentially fixes this, from what I read

@rht rht force-pushed the union_operator branch from 8ef2176 to 9bca27d Compare May 27, 2022 08:03
@rht
Copy link
Contributor Author

rht commented May 27, 2022

TimeT: TypeAlias = float | int potentially fixes this, from what I read

I tried this in the 2nd commit. For Python < 3.10, failed with the following error message:

E   ImportError: cannot import name 'TypeAlias' from 'typing' 

1 similar comment
@rht
Copy link
Contributor Author

rht commented May 27, 2022

TimeT: TypeAlias = float | int potentially fixes this, from what I read

I tried this in the 2nd commit. For Python < 3.10, failed with the following error message:

E   ImportError: cannot import name 'TypeAlias' from 'typing' 

@rht
Copy link
Contributor Author

rht commented May 27, 2022

I think I will use Union for the type aliases for now. Only 4 lines of code need them. The rest can be safely set to use |.

@rht rht force-pushed the union_operator branch from 03419e1 to 38d662e Compare May 27, 2022 11:47
@rht rht marked this pull request as ready for review May 27, 2022 11:49
@rht
Copy link
Contributor Author

rht commented May 27, 2022

PR is ready for review.

Copy link
Member

@tpike3 tpike3 left a comment

Choose a reason for hiding this comment

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

LGTM

@tpike3 tpike3 merged commit 9a78551 into projectmesa:main Jun 4, 2022
@rht rht deleted the union_operator branch June 4, 2022 12:08
@jackiekazil jackiekazil added this to the Quartzsite (next release) milestone Jun 15, 2022
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.

5 participants