FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

`collections`: Add missing reflected BinOp methods by AlexWaygood · Pull Request #7207 · python/typeshed · GitHub

Repository navigation

collections: Add missing reflected BinOp methods - #7207

Merged
JelleZijlstra merged 4 commits into
python:masterfrom
AlexWaygood:binops
Feb 14, 2022
Merged

JelleZijlstra merged 4 commits into
python:masterfrom
AlexWaygood:binops

Conversation

Copy link
Copy Markdown
Member

Testing confirms that mypy currently raises false positives for these operations, whereas they all succeed at runtime.

This comment has been minimized.

Comment thread stdlib/collections/__init__.pyi Outdated
def __mul__(self: Self, n: int) -> Self: ...
def __rmul__(self: Self, n: int) -> Self: ...
def __mod__(self: Self, args: Any) -> Self: ...
def __rmod__(self: Self, template: object) -> Self: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This one is actually broken:

In [12]: [] % collections.UserString("x")

Traceback (most recent call last):
  File "/main_instance_shell/jelle/venv/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-12-0ca5ac41c5a2>", line 1, in <module>
    [] % collections.UserString("x")
  File "/main_instance_shell/jelle/venv/lib/python3.6/collections/__init__.py", line 1162, in __rmod__
    return self.__class__(format % args)
NameError: name 'args' is not defined

It should error but not with a NameError.

AlexWaygood Feb 14, 2022 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Re the NameError, do you wanna file the BPO issue or shall I? :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Go ahead! I think this method just shouldn't exist. It's presumably there to support %-formatting, but I don't think a reflected method makes sense for that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Wait, I can't reproduce the NameError. This is what I get on my machine:

>>> from collections import UserString
>>> [] % UserString('foo')
'[]'
>>> [] % UserString('x')
'[]'
>>>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ah, it got fixed in https://bugs.python.org/issue25652. That's what I get for trying things out on 3.6. I admit I don't understand the reasoning they ended up with for keeping the method.

AlexWaygood Feb 14, 2022 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

collections.UserString does feel like it was written during a phase when Raymond Hettinger was incredibly hyped about Javascript or something:

>>> 5 + UserString('foo')
'5foo'
>>> NotImplemented + UserString('foo')
'NotImplementedfoo'

Like... wut...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
In [17]: collections.UserString("x") + collections.UserList(["y"])
Out[17]: "x['y']"

In [18]: collections.UserList(["y"]) + collections.UserString("x")
Out[18]: ['y', 'x']

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Truly a nightmare of what Python could have been

Comment thread stdlib/collections/__init__.pyi Outdated
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>

This comment has been minimized.

Copy link
Copy Markdown
Member

The mypy-primer errors are here: https://github.com/sphinx-doc/sphinx/blob/4.x/sphinx/locale/__init__.py#L67. They're already type-ignoring similar issues with __add__, so it seems fine to produce the same error on __radd__.

Copy link
Copy Markdown
Contributor

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

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/locale/__init__.py: note: In member "__radd__" of class "_TranslationProxy":
+ sphinx/locale/__init__.py:70:5: error: Argument 1 of "__radd__" is incompatible with supertype "UserString"; supertype defines the argument type as "object"
+ sphinx/locale/__init__.py:70:5: note: This violates the Liskov substitution principle
+ sphinx/locale/__init__.py:70:5: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
+ sphinx/locale/__init__.py:70:5: error: Return type "str" of "__radd__" incompatible with return type "_TranslationProxy" in supertype "UserString"
+ sphinx/locale/__init__.py: note: In member "__rmod__" of class "_TranslationProxy":
+ sphinx/locale/__init__.py:76:5: error: Return type "str" of "__rmod__" incompatible with return type "_TranslationProxy" in supertype "UserString"
+ sphinx/locale/__init__.py: note: In member "__rmul__" of class "_TranslationProxy":
+ sphinx/locale/__init__.py:82:5: error: Return type "str" of "__rmul__" incompatible with return type "_TranslationProxy" in supertype "UserString"

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL