Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Delete sametypes.py #13311

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 9 commits into from
Aug 4, 2022
Merged

Conversation

ilevkivskyi
Copy link
Member

Fixes #7924

So this is not 100% refactor, I make several semantic changes. As discussed in the issue, current implementation is not very principled, so I replace old complex logic with two options:

  • New is_same_types() is now located in subtypes.py and is doing two-way proper subtyping check.
  • Type equality with == can be used when we are checking for representation equality (used rarely, mostly for internal things)

Btw this uncovered two actual bugs, one of which I fix here (two-line change), and leave a TODO for the second. Let's keep fingers crossed for mypy_primer.

cc @JukkaL

@ilevkivskyi
Copy link
Member Author

@hauntsaninja @JelleZijlstra could one of you (or both) please take a look at this one as well?

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

ilevkivskyi commented Aug 2, 2022

OK, so mypy_primer is expected, I effectively slightly relaxed rules for --warn-redundant-casts, and slightly enforced them for assert_type(). I used this as a rule of thumb when deciding whether to switch to ==: do I want int and int64 be different under this check? (They are proper subtypes of each other, but have different runtime representation.)

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
Member Author

@JukkaL After some thinking, I decided to use ignore_promotions=True for the new implementation of is_same_type(), so e.g. int and int64 will be different and Union[int, bool] (if not simplified) will be different from int. This is somewhat closer to old behavior, although there is no difference in tests and in mypy_primer.

I went again through all call sites (there is just a dozen or so), and I think the semantics introduced by this PR makes sense there.
I remember you was quite cautious about this change few years ago. But with mypy_primer I am more confident now. If you are still against, please let me know, otherwise I will merge this soon.

@ilevkivskyi
Copy link
Member Author

After working a bit on cleaning get_proper_type() usage, I think I will use is_same_type() in assert_type() (like it was before). Having == there will be a big pain for users if type is recursive alias.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2022

Diff from mypy_primer, showing the effect of this PR on open source code:

spark (https://github.com/apache/spark)
- python/pyspark/sql/pandas/types.py:351: error: Redundant cast to PandasSeriesLike?  [redundant-cast]
- python/pyspark/sql/pandas/types.py:412: error: Redundant cast to PandasSeriesLike?  [redundant-cast]
- python/pyspark/sql/pandas/types.py:422: error: Redundant cast to PandasSeriesLike?  [redundant-cast]

cwltool (https://github.com/common-workflow-language/cwltool)
- cwltool/main.py: note: In function "main":
- cwltool/main.py:1128:26: error: Redundant cast to "Union[Any, Any]"  [redundant-cast]

steam.py (https://github.com/Gobot1234/steam.py)
- steam/package.py:227: error: Unsupported left operand type for & (Self?)  [operator]

core (https://github.com/home-assistant/core)
- homeassistant/components/simplisafe/__init__.py:255: error: Redundant cast to "Union[Any, Any]"  [redundant-cast]

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.

Should we remove sametypes.py?
2 participants