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

Introduce B009 and B010 #55

Merged
merged 1 commit into from
Nov 6, 2018
Merged

Conversation

joshkehn
Copy link
Contributor

@joshkehn joshkehn commented Nov 3, 2018

Using getattr(x, 'foo') and setattr(x, 'foo', 'bar') are no more
safe than normal property access and probably cause additional confusion
about safety when they are used because without a default getattr
raises the same AttributeError as dot access. There is also a
performance cost associated with doing attribute access indirectly.

I've added these as separate warnings since they opperate on two
different behaviors but they could probably be combined as a single
"fat" warning without trouble.

Example with getattr:

>>> import os
>>> getattr(os, "foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'os' has no attribute 'foo'

Example with setattr:

>>> os.foo = "bar"
>>> os.foo
'bar'
>>> setattr(os, "baz", "bar")
>>> os.baz
'bar'

@joshkehn joshkehn force-pushed the b009-b010-getattr-setattr branch 3 times, most recently from 888f6e1 to 9a0ac57 Compare November 3, 2018 20:29
README.rst Outdated Show resolved Hide resolved
Using `getattr(x, 'foo')` and `setattr(x, 'foo', 'bar')` are no more
safe than normal property access and probably cause additional confusion
about safety when they are used because without a default `getattr`
raises the same `AttributeError` as dot access. There is also a
performance cost associated with doing attribute access indirectly.

I've added these as separate warnings since they opperate on two
different behaviors but they could probably be combined as a single
"fat" warning without trouble.

Example with `getattr`:

```python
>>> import os
>>> getattr(os, "foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'os' has no attribute 'foo'
```

Example with `setattr`:

```python
>>> os.foo = "bar"
>>> os.foo
'bar'
>>> setattr(os, "baz", "bar")
>>> os.baz
'bar'
```
@ambv ambv merged commit d944471 into PyCQA:master Nov 6, 2018
@ambv
Copy link
Member

ambv commented Nov 6, 2018

Thanks! ✨ 🍰 ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants