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

Support External Classes' __rtruediv__ #831

Merged
merged 2 commits into from
Apr 19, 2023

Conversation

michaeljpeters
Copy link
Contributor

@michaeljpeters michaeljpeters commented Mar 10, 2023

What do these changes do?

Allows external classes to extend functionality properly using __rtruediv__.

Previously, URL would raise a not-so-nice error like this:

>>> from yarl import URL
>>> 
>>> u = URL('https://beefslab.com/abc')
>>> u
URL('https://beefslab.com/abc')
>>> u / 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/peters/.env38/lib/python3.8/site-packages/yarl/_url.py", line 318, in __truediv__
    name = self._PATH_QUOTER(name)
  File "yarl/_quoting_c.pyx", line 213, in yarl._quoting_c._Quoter.__call__
TypeError: Argument should be str

Now, it will properly return NotImplemented (still eventually resulting in a TypeError).

>>> u / 3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'URL' and 'int'

https://docs.python.org/3/library/constants.html#NotImplemented
https://docs.python.org/3/reference/datamodel.html#object.__rtruediv__
"These functions are only called if the left operand does not support the corresponding operation"

Are there changes in behavior for the user?

No

Related issue number

#832

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes - Not added, minor change
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> (e.g. 588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the PR
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: Fix issue with non-ascii contents in doctest text files.

@michaeljpeters michaeljpeters changed the title return NotImplemented when __truediv__ is called with a non-string Support External Classes' __rtruediv__ Mar 10, 2023
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Mar 10, 2023
@michaeljpeters michaeljpeters marked this pull request as ready for review March 10, 2023 02:30
@mjpieters mjpieters enabled auto-merge (squash) April 19, 2023 16:32
def test_div_bad_type():
url = URL("http://example.com/path/")
with pytest.raises(TypeError):
url / 3

Check notice

Code scanning / CodeQL

Statement has no effect

This statement has no effect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants