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

False positive for WPS125 and attributes #1327

Closed
sobolevn opened this issue Apr 10, 2020 · 10 comments
Closed

False positive for WPS125 and attributes #1327

sobolevn opened this issue Apr 10, 2020 · 10 comments
Labels
bug Something isn't working help wanted Extra attention is needed level:starter Good for newcomers

Comments

@sobolevn
Copy link
Member

Bug report

What's wrong

  76:1     WPS125 Found builtin shadowing: __doc__
  _coalesce_result.__doc__ = __doc__
  ^

It is not technically builtin shadowing.

@sobolevn sobolevn added the bug Something isn't working label Apr 10, 2020
@sobolevn sobolevn added this to the Version 0.15 aka New runtime milestone Apr 17, 2020
@sobolevn sobolevn added help wanted Extra attention is needed level:starter Good for newcomers labels Apr 17, 2020
@Dreamsorcerer
Copy link
Contributor

Also:

class Foo:
    id = 5

@skarzi
Copy link
Collaborator

skarzi commented Jul 19, 2020

Example provided by @Dreamsorcerer is really common especially when working with ORMs, it's quite common to name fields/descriptors id or type, so it would be also great enhancement 👍

@skarzi
Copy link
Collaborator

skarzi commented Jul 21, 2020

Even following one:

def foo(bar):
    bar.id = 12345

@orsinium
Copy link
Collaborator

This is the rule that is hard to have done well, IMHO.

  1. It's ok to shadow some built-ins. Namely: credits, copyright, license.
  2. It's ok to shadow any built-in on the class attributes level because of ORM and data classes.
  3. And it's not ok at the same time:
    class A:
        min = Field(...)
        max = Field(default=min(1,2,3))
    it raises something like 'field' object is not callable

Python 🤷

@Dreamsorcerer
Copy link
Contributor

I think 3 is an edge case that's better handled by type checking (e.g. Mypy), rather than worrying about it in wemake.
The error can only occur within the definition of the class attributes, so it's not something that's likely to happen very often.

@Dreamsorcerer
Copy link
Contributor

Alternatively, a single # noqa when defining the attribute would be acceptable. As long as using the attribute later on doesn't cause a violation as in the other examples (e.g. A.min = 5).

@Dreamsorcerer
Copy link
Contributor

OK, thinking about it, I think the latter is the better answer, have opened a PR to add this information to the docs.

@sobolevn
Copy link
Member Author

sobolevn commented Feb 7, 2021

Ok, we have lots of bugs with attributes here.

First of all, I want to change how some rules are applied for not self/cls/mcs targets.
For example, point.x = 1 should not be a length violation, because point might have this specific API.
Also, we don't want to raise violations on things like @Dreamsorcerer mentioned in #1327 (comment)

I also want to ignore license and some other names mentioned by @orsinium in #1327 (comment)

So, I will refector a lot of stuff to get this correct. Future reviews are welcome!

@sobolevn
Copy link
Member Author

sobolevn commented Feb 7, 2021

So, basically we would have two sets of rules for attributes:

  • For all attributes, for example [any].__prop = 1 is a violation (a couple of rules fit into this category)
  • For self*-ish attributes, where self.x = 1 is a violation (almost all our rules will be in this category)

@sobolevn
Copy link
Member Author

sobolevn commented Feb 7, 2021

I am not going to create a new issue for it, because I am working on it right now anyways, but this is also a bug:

class Some(object):
    UPPER, lower = 1, 2

It does not raise any violations, but it should raise:

  2:5      WPS115 Found upper-case constant in a class: UPPER
  UPPER = 1

sobolevn added a commit that referenced this issue Feb 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed level:starter Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants