Skip to content

Commit

Permalink
Add __call__ to list of methods that should be on top wemake-servic…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilles Igot committed Feb 2, 2020
1 parent 606db70 commit b398ede
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Semantic versioning in our case means:

- Extracts new violation - WPS450 from WPS436 #1118
- Adds domain names options, that are used to create variable names' blacklist #1106
- Add `__call__` to list of methods that should be on top #1125

### Bugfixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def __new__(self):
def __init__(self):
...
def __call__(self):
...
def public(self):
...
Expand Down Expand Up @@ -79,19 +82,25 @@ def test_correct_method_order(

@pytest.mark.parametrize(('first', 'second'), [
('__init__', '__new__'),
('__call__', '__init__'),
('__call__', '__new__'),
('public', '__new__'),
('public', '__init__'),
('public', '__call__'),
('__magic__', '__new__'),
('__magic__', '__init__'),
('__magic__', '__call__'),
('_protected', '__new__'),
('_protected', '__init__'),
('_protected', '__call__'),
('_protected', 'public'),
('_protected', '__magic__'),
('__private', '__new__'),
('__private', '__init__'),
('__private', '__call__'),
('__private', 'public'),
('__private', '__magic__'),
('__private', '_protected'),
Expand Down
1 change: 1 addition & 0 deletions wemake_python_styleguide/violations/consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,7 @@ class WrongMethodOrderViolation(ASTViolation):
- ``__new__``
- ``__init__``
- ``__call__``
- public and magic methods
- protected methods
- private methods (we discourage using them)
Expand Down
13 changes: 8 additions & 5 deletions wemake_python_styleguide/visitors/ast/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,15 @@ def _check_method_order(self, node: ast.ClassDef) -> None:
return

def _ideal_order(self, first: str) -> int:
if first == '__new__':
return 4 # highest priority
if first == '__init__':
return 3
base_methods_order = {
'__new__': 5, # highest priority
'__init__': 4,
'__call__': 3,
}
public_magic_methods_priority = 2

if access.is_protected(first):
return 1
if access.is_private(first):
return 0 # lowest priority
return 2 # public and magic methods
return base_methods_order.get(first, public_magic_methods_priority)

0 comments on commit b398ede

Please sign in to comment.