-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Document that CPython accepts "invalid" identifiers #79286
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
Comments
The Python 3 language has a strict definition for an identifier: ... but in practice, it's possible to create "invalid" identifiers using setattr(). Example with PyPy: $ pypy
Python 2.7.13 (0e7ea4fe15e82d5124e805e2e4a37cae1a402d4b, Apr 12 2018, 14:50:12)
>>>> class A: pass
>>>>
>>>> a=A()
>>>> setattr(a, "1", 2)
>>>> getattr(a, "1")
2 >>>> vars(a)
{'1': 2}
>>>> a.__dict__
{'1': 2}
>>>> a.1
File "<stdin>", line 1
a.1
^
SyntaxError: invalid syntax The exact definition of "identifiers" is a common question. Recent examples:
It would be nice to document the answer. Maybe in the Langage Specification, maybe in the setattr() documentation, maybe in a FAQ, maybe everywhere? |
I'd argue that it's an implementation detail. Documenting it might be nice as some projects such as pytest do use it but I don't think it would make sense in setattr() or getattr() since all they do (at least in this case) is assign/retrieve from the __dict__. One thing to note is that __slots__ doesn't accept them. |
Maybe it's obvious to you, but the question is asked again and again, at least once per year. So it seems like we need to document it somewhere. |
I agreed we should document it, it' not obvious to me at least. |
The customizing attribute access section of the data model might be a suitable place. |
It is an implementation detail that some people need to know, and that is very unlikely to change. In the pydev thread, Guido said We occasionally document such things in a 'CPython implementation detail' note. I don't know the proper markup for these. At present, I think the note should be in setattr and **kwargs docs. |
It's ".. impl-detail::". See for example: |
This seems like a confusion of two things: identifiers are lexical elements of the language. Attributes are not limited to identifiers. We could add to the docs for setattr: "The attribute name does not have to be a valid identifier." I don't know what the language guarantees about what strings are valid as attribute names. |
But in a later message, after additional discussion, he acknowledged there could be reasons to change and said, "we needn't rush this." So if the docs do describe the current implementation, I think it should warn people that this behavior might not be subject to the same backwards compatibility guarantees as other documented behavior. |
Documenting something as an 'implementation detail' denies that it is a language feature and does not offer stability guarantees. |
I take back my previous suggestion, I agree that documenting it in setattr() (and **kwargs) is the way to go. It's obvious that you can assign anything to the __dict__, since it represents a dict, but setattr() is more ambiguous. |
I try to create a PR for it. Should we add 'CPython implementation detail' at the document? Because this happens at cpython as well as pypy. BTW, where should we add the document? I have two choices. |
Any ideas? Or I will create a PR in a week without 'CPython implementation detail' |
I don't think we want to give any stability guarantees for this. Perhaps I would be happy for it to be stated as an CPython implementation Please go ahead and make a PR. |
I don't think we can mark this as an implementation detail for setattr(). The details are downstream and determined by the target object, not by setattr() itself. Suggested wording: ''' In short, we can talk about this in the setattr() docs but it isn't really a setattr() issue. Also, the behavior is effectively guaranteed by the other things users are allowed to do, so there is no merit in marking this as an implementation detail. Non-identifier keys can make it into an instance dictionary via multiple paths that are guaranteed to work. |
FWIW, the only restriction added by setattr() is that *name* must be a string. |
I agreed with @Raymond Hettinger, I will update the PR from your suggestion if no other ideas in next week. |
Just as another edge case, type() can do the same thing: Foo = type("Foo", (object,), {"a b": 1})
f = Foo() for example, will create a class attribute named "a b". Maybe this actually calls setattr() under the covers, but if it's going to be documented, it should be noted in both places. |
It seems like the documentation is lacking and perhaps misleading in regard to attributes.
Is the reason for allowing that, - only performance (obviously that's a strong enough reason in this case)? Or can this be useful in some other corner cases?
In addition to just being confusing, I think this can create an impression for users that setattr() allows you to set 'private' or 'hidden' attrs, and setting attrs via __dict__ allows you to set even more 'private', 'top secret' attrs. Since attributes are such a core concept in Python, it might be good to have a section that lays out all of these corner cases and reasons for them, so that it can be linked from docs for setattr(), __dict__, dir(), inspect.getmembers(), etc. |
In the last message I've said that according to __dict__ docs, anything in __dict__ is an attribute of respective obj. That's a bit too-strongly worded, the docs can be understood in the sense that anything that ends up in __dict__ via other mechanisms, such as dotted notation or setattr(), is an attribute. Since direct manipulation of __dict__ is not prohibited, and no limits are set, AFAIK, on keys that can be used for __dict__, the more natural reading of the docs is that anything that can be directly set in __dict__ is also an attribute. The only thing that would make a user doubt this reading is if he or she finds that getattr() cannot get non-string attrs, and going by its name, user would assume you can get any valid attrs using getattr(). |
@gvanrossum, it looks like this issue is an older version of Do you think there's anything left to do here now that #96393 has been merged? |
Let's close this issue, it was independently discovered and solved (by updating the docs). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: