This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.smith
Recipients eric.smith, mickey695
Date 2017-10-31.07:43:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1509435797.39.0.213398074469.issue31907@psf.upfronthosting.co.za>
In-reply-to
Content
How would you distinguish this from the case where an actually missing attribute was given?

>>> "{0.ctimex}".format(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute 'ctimex'

I guess it would be possible to change the parser to only look for valid chars in an identifier and give an error if there were "extra" chars found, but I'm not sure it's worth the hassle. The error message seems clear enough to me. Also, I wouldn't want to change this to not be an AttributeError (for backward compatibility), and I'm not sure an AttributeError with a different message would be good.

Of course, this works with f-strings:

>>> f"{d.ctime()}"
'Tue Oct 31 00:00:00 2017'

And I just realized that your code actually is valid, with a custom __getattr__. And while I wouldn't recommend doing this, I also wouldn't want to break it. For all I know, people have written custom evaluators in __getattr__:

>>> class C:
...     def __getattr__(self, name):
...         if name == 'ctime()':
...             return 'okay'
...         raise AttributeError('not found')
...

>>> '{0.x}'.format(C())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in __getattr__
AttributeError: not found

>>> '{0.ctime()}'.format(C())
'okay'

I'm changing versions because this would be a new feature, since it breaks valid code.
History
Date User Action Args
2017-10-31 07:43:17eric.smithsetrecipients: + eric.smith, mickey695
2017-10-31 07:43:17eric.smithsetmessageid: <1509435797.39.0.213398074469.issue31907@psf.upfronthosting.co.za>
2017-10-31 07:43:17eric.smithlinkissue31907 messages
2017-10-31 07:43:16eric.smithcreate