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

gh-96663: Add a better error message for __dict__-less classes setattr #103232

Merged
merged 11 commits into from
Jul 22, 2023

Conversation

Gobot1234
Copy link
Contributor

@Gobot1234 Gobot1234 commented Apr 4, 2023

I think this addresses all the issues I have with the current message. Thanks to Eryk for the pointer as to where I should be editing.

@arhadthedev arhadthedev added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Apr 4, 2023
Objects/object.c Outdated
Comment on lines 1582 to 1583
"'%.100s' object has no attribute '%U' and no "
"__dict__ for setting new attributes",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't find the tests for this bit of code, are there any?

Copy link
Contributor

@thatbirdguythatuknownot thatbirdguythatuknownot left a comment

Choose a reason for hiding this comment

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

There's an example that raises the error message when a __dict__ is available, so there has to be a check in the second path.

Objects/object.c Outdated Show resolved Hide resolved
@Gobot1234
Copy link
Contributor Author

I'm not entirely sure why the tests are failing

>>> class B:
...     y = 0
...     __slots__ = ('z', 'foo')
... 
>>> B().fod = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'B' object has no attribute 'fod' and no __dict__ for setting new attributes. Did you mean: 'foo'?

AFAICT this should be the error that you get? Unless it's failing due to not being printed.

@thatbirdguythatuknownot
Copy link
Contributor

thatbirdguythatuknownot commented Apr 4, 2023

I'm not entirely sure why the tests are failing

>>> class B:
...     y = 0
...     __slots__ = ('z', 'foo')
... 
>>> B().fod = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'B' object has no attribute 'fod' and no __dict__ for setting new attributes. Did you mean: 'foo'?

AFAICT this should be the error that you get? Unless it's failing due to not being printed.

@Gobot1234 The suggestion (Did you mean: 'foo'?) does not seem to be part of the error.

@ionite34
Copy link
Contributor

ionite34 commented Apr 4, 2023

Should this perhaps check for __setattr__ overrides? There can be cases where the class has no __dict__ but some non slot attribute setting can still happen. In such a case this error might be misleading.

class Foo:
  __slots__ = ("x", "y")
	
  def __setattr__(self, name, val):
    if name == "all":
      self.x = val
      self.y = val
    else:
      super().__setattr__(name, val)
	
f = Foo()
f.all = 5

Co-authored-by: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com>
@Gobot1234
Copy link
Contributor Author

@pablogsal since this is an error message change would you be willing to review?

Lib/test/test_descrtut.py Outdated Show resolved Hide resolved
@gvanrossum gvanrossum merged commit cdeb1a6 into python:main Jul 22, 2023
@Gobot1234 Gobot1234 deleted the better-error-for-no-dict branch July 22, 2023 12:43
cjwatson added a commit to cjwatson/testfixtures that referenced this pull request Nov 7, 2024
Start testing on 3.13, and fix a test for the change introduced in
python/cpython#103232.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants