Today I saw this code:
def __init__(self, tree, filename='(none)', builtins=None,
withDoctest='PYFLAKES_DOCTEST' in os.environ, tokens=()):
What it does? It uses an expression as a default value. I am strongly against that.
We should forbid anything that is not a ast.Name or ast.Attribute to be used as a default value.
It can be tricky. Nothing stops you from making database calls or http requests in such expressions.
It is also not readable for me. I would prefer to write:
SHOULD_USE_DOCTEST = 'PYFLAKES_DOCTEST' in os.environ
def __init__(self, tree, filename='(none)', builtins=None,
withDoctest=SHOULD_USE_DOCTEST, tokens=()):
At the same time are you fine with pytest.mark.skipIf and pytest.mark.xfail cases? How they are different from this one?
I`ll do it!
It will be a challenge for you, because everything is new to me.
I'm a little confused. I realized that I need to add a violation, but I鈥檓 not sure that I need to add a new visitor (I continue to think about this pattern).
I think the correct base class for this violation is ASTViolation. Am i right? I think i should add the new violation to "best_practises"? But i can not figure out where to add and how.
Could you direct me?
@mikhail-akimov yes, you are absolutly correct.
You need to create:
ast violation in best_practices.py_check_argument_default_valuesI found out that in order to prohibit the expression in default, it is enough to forbid ast.Compare, but what about ast.Tuple, ast.NameConstant, ast.Tuple? We have to forbid them too?
What then will be the correct example?
The rule is: we should allow any immutable simple values and variables:
None, True, False1, 2.0'some string', b'some data string'(1, 2)variable_name, module.CONTANT
Most helpful comment
I`ll do it!