59 lines
1.4 KiB
Plaintext
59 lines
1.4 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Accessing a non-existing member on an object will raise in most case an ``++AttributeError++`` exception.
|
|
|
|
|
|
This rule raises an issue when a non-existing member is accessed on a class instance and nothing indicates that this was expected.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,python]
|
|
----
|
|
def access_attribute():
|
|
x = 42
|
|
return x.isnumeric() # Noncompliant
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,python]
|
|
----
|
|
def access_attribute():
|
|
x = "42"
|
|
return x.isnumeric()
|
|
----
|
|
|
|
|
|
=== Exceptions
|
|
|
|
No issue will be raised:
|
|
|
|
* in ``++try ... except++`` blocks when ``++AttributeError++`` is caught.
|
|
* in ``++if hasattr(...)++`` blocks.
|
|
* when the accessed object has methods ``++__getattribute__++`` or ``++__getattr__++``. These methods will override the default attribute access behavior.
|
|
* when a class or its parent classes use ``++setattr++``. This can be used to add dynamically members to the class, even if it is not recommended.
|
|
|
|
You can also disable any issue for a given class by adding it to this rule's ignoredClassesForMemberValidation property.
|
|
|
|
|
|
== Resources
|
|
|
|
* https://youtu.be/NiSqG6s8skA[The Magic of Attribute Access - by Petr Viktorin - EuroPython Conference 2014]
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::parameters.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|