Eric V. Smith <e...@trueblade.com> added the comment:

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.

----------
assignee:  -> eric.smith
nosy: +eric.smith
status: open -> pending
versions:  -Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue31907>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to