Skip to content

Conversation

@AlexWaygood
Copy link
Member

This is a third attempt to give more type specificity to the binary comparison functions in operator, after failed attempts in #6449 and #6448.

This attempt gives no more specificity to the return type of these functions (sadly), but does help constrain the types of the parameters. Currently, the following snippet of code passes mypy, but fails at runtime:

lt(object(), 6)

With this PR, mypy correctly rejects this.

Snippets like the following still continue to raise no errors, however:

lt('foo', 6)

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Nov 30, 2021

It might be preferable for type-checkers to special-case these functions, at the end of the day. mypy's clever enough to know that "foo" < 6 should raise an error; it should probably be clever enough to know that the same logic should apply to lt("foo", 6). Interested to hear what others think, however.

^Type-checkers might have difficulty special-casing these functions, because they might well run into the same false-positives I did in #6448

@github-actions

This comment has been minimized.

@AlexWaygood AlexWaygood reopened this Nov 30, 2021
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Dec 1, 2021

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

I believe these may all be true positives now?

@AlexWaygood AlexWaygood closed this Dec 1, 2021
@AlexWaygood AlexWaygood reopened this Dec 1, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Dec 1, 2021

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

pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/indexes/base.py:3854: error: Argument 1 has incompatible type "Union[ExtensionArray, ndarray[Any, Any]]"; expected "Union[_SupportsLE, _SupportsGE, _SupportsGT, _SupportsLT]"  [arg-type]
+ pandas/core/indexes/base.py:3854: error: Argument 2 has incompatible type "Union[ExtensionArray, ndarray[Any, Any]]"; expected "Union[_SupportsLE, _SupportsGE, _SupportsGT, _SupportsLT]"  [arg-type]
+ pandas/io/formats/style.py:3502: error: Argument 2 to "ge" has incompatible type "Union[str, Any, float, Period, Timestamp, Timedelta, Sequence[Any], ndarray[Any, Any], NDFrame]"; expected "Union[_SupportsLE, _SupportsGE, _SupportsGT, _SupportsLT]"  [arg-type]
+ pandas/io/formats/style.py:3507: error: Argument 2 to "le" has incompatible type "Union[str, Any, float, Period, Timestamp, Timedelta, Sequence[Any], ndarray[Any, Any], NDFrame]"; expected "Union[_SupportsLE, _SupportsGE, _SupportsGT, _SupportsLT]"  [arg-type]

@JelleZijlstra JelleZijlstra merged commit 6246e78 into python:master Dec 1, 2021
@AlexWaygood AlexWaygood deleted the improve-operator-2 branch December 1, 2021 07:19
@AlexWaygood AlexWaygood mentioned this pull request Dec 1, 2021
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