diff options
Diffstat (limited to 'tests/functional/n/name')
-rw-r--r-- | tests/functional/n/name/namePresetCamelCase.py | 23 | ||||
-rw-r--r-- | tests/functional/n/name/namePresetCamelCase.rc | 13 | ||||
-rw-r--r-- | tests/functional/n/name/namePresetCamelCase.txt | 3 | ||||
-rw-r--r-- | tests/functional/n/name/name_good_bad_names_regex.py | 20 | ||||
-rw-r--r-- | tests/functional/n/name/name_good_bad_names_regex.rc | 15 | ||||
-rw-r--r-- | tests/functional/n/name/name_good_bad_names_regex.txt | 3 | ||||
-rw-r--r-- | tests/functional/n/name/name_preset_snake_case.py | 35 | ||||
-rw-r--r-- | tests/functional/n/name/name_preset_snake_case.rc | 14 | ||||
-rw-r--r-- | tests/functional/n/name/name_preset_snake_case.txt | 5 | ||||
-rw-r--r-- | tests/functional/n/name/name_styles.py | 156 | ||||
-rw-r--r-- | tests/functional/n/name/name_styles.rc | 5 | ||||
-rw-r--r-- | tests/functional/n/name/name_styles.txt | 18 | ||||
-rw-r--r-- | tests/functional/n/name/namedtuple_member_inference.py | 22 | ||||
-rw-r--r-- | tests/functional/n/name/namedtuple_member_inference.txt | 1 | ||||
-rw-r--r-- | tests/functional/n/name/names_in__all__.py | 49 | ||||
-rw-r--r-- | tests/functional/n/name/names_in__all__.txt | 6 |
16 files changed, 388 insertions, 0 deletions
diff --git a/tests/functional/n/name/namePresetCamelCase.py b/tests/functional/n/name/namePresetCamelCase.py new file mode 100644 index 000000000..e048ec4d8 --- /dev/null +++ b/tests/functional/n/name/namePresetCamelCase.py @@ -0,0 +1,23 @@ +# pylint: disable=missing-docstring,too-few-public-methods +__version__ = "1.0" +SOME_CONSTANT = 42 # [invalid-name] + + +def sayHello(someArgument): + return [someArgument * someValue for someValue in range(10)] + + +class MyClass: # [invalid-name] + def __init__(self, argX): + self._mySecretX = argX + + @property + def myPublicX(self): + return self._mySecretX * 2 + + def __eq__(self, other): + return isinstance(other, MyClass) and self.myPublicX == other.myPublicX + + +def say_hello(): # [invalid-name] + pass diff --git a/tests/functional/n/name/namePresetCamelCase.rc b/tests/functional/n/name/namePresetCamelCase.rc new file mode 100644 index 000000000..29f44504a --- /dev/null +++ b/tests/functional/n/name/namePresetCamelCase.rc @@ -0,0 +1,13 @@ +[BASIC] +function-naming-style=camelCase +const-naming-style=camelCase +attr-naming-style=camelCase +argument-naming-style=camelCase +module-naming-style=camelCase +method-naming-style=camelCase +variable-naming-style=camelCase +class-attribute-naming-style=camelCase +inlinevar-naming-style=camelCase +class-naming-style=camelCase + +include-naming-hint=yes diff --git a/tests/functional/n/name/namePresetCamelCase.txt b/tests/functional/n/name/namePresetCamelCase.txt new file mode 100644 index 000000000..b24b0a881 --- /dev/null +++ b/tests/functional/n/name/namePresetCamelCase.txt @@ -0,0 +1,3 @@ +invalid-name:3:0::"Constant name ""SOME_CONSTANT"" doesn't conform to camelCase naming style ('([^\\W\\dA-Z][^\\W_]*|__.*__)$' pattern)" +invalid-name:10:0:MyClass:"Class name ""MyClass"" doesn't conform to camelCase naming style ('[^\\W\\dA-Z][^\\W_]+$' pattern)" +invalid-name:22:0:say_hello:"Function name ""say_hello"" doesn't conform to camelCase naming style ('([^\\W\\dA-Z][^\\W_]{2,}|__[^\\W\\dA-Z_]\\w+__)$' pattern)" diff --git a/tests/functional/n/name/name_good_bad_names_regex.py b/tests/functional/n/name/name_good_bad_names_regex.py new file mode 100644 index 000000000..f1ed4b91b --- /dev/null +++ b/tests/functional/n/name/name_good_bad_names_regex.py @@ -0,0 +1,20 @@ +# pylint: disable=missing-docstring,too-few-public-methods +__version__ = "1.0" +ignored_SOME_CONSTANT = 42 + +explicit_bad_some_constant = 42 # [blacklisted-name] + +snake_case_bad_SOME_CONSTANT = 42 # [invalid-name] + + +class my_class: + def __init__(self, arg_x): + self._my_secret_x = arg_x + + @property + def my_public_x(self): + return self._my_secret_x * 2 + + +def blacklisted_2_snake_case(): # [blacklisted-name] + pass diff --git a/tests/functional/n/name/name_good_bad_names_regex.rc b/tests/functional/n/name/name_good_bad_names_regex.rc new file mode 100644 index 000000000..8246438cd --- /dev/null +++ b/tests/functional/n/name/name_good_bad_names_regex.rc @@ -0,0 +1,15 @@ +[BASIC] +function-naming-style=snake_case +const-naming-style=snake_case +attr-naming-style=snake_case +argument-naming-style=snake_case +module-naming-style=snake_case +method-naming-style=snake_case +variable-naming-style=snake_case +class-attribute-naming-style=snake_case +inlinevar-naming-style=snake_case +class-naming-style=snake_case + +good-names-rgxs=ignored.* +bad-names-rgxs=explicit.*,blacklisted.* +include-naming-hint=yes diff --git a/tests/functional/n/name/name_good_bad_names_regex.txt b/tests/functional/n/name/name_good_bad_names_regex.txt new file mode 100644 index 000000000..08aef7d78 --- /dev/null +++ b/tests/functional/n/name/name_good_bad_names_regex.txt @@ -0,0 +1,3 @@ +blacklisted-name:5:0::Black listed name "explicit_bad_some_constant" +invalid-name:7:0::"Constant name ""snake_case_bad_SOME_CONSTANT"" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]*|__.*__)$' pattern)" +blacklisted-name:19:0:blacklisted_2_snake_case:Black listed name "blacklisted_2_snake_case" diff --git a/tests/functional/n/name/name_preset_snake_case.py b/tests/functional/n/name/name_preset_snake_case.py new file mode 100644 index 000000000..75350497e --- /dev/null +++ b/tests/functional/n/name/name_preset_snake_case.py @@ -0,0 +1,35 @@ +# pylint: disable=missing-docstring,too-few-public-methods +from enum import Enum +from typing import ClassVar + +__version__ = "1.0" +SOME_CONSTANT = 42 # [invalid-name] + + +def say_hello(some_argument): + return [some_argument * some_value for some_value in range(10)] + + +class MyClass: # [invalid-name] + def __init__(self, arg_x): + self._my_secret_x = arg_x + + @property + def my_public_x(self): + return self._my_secret_x * 2 + + def __eq__(self, other): + return isinstance(other, MyClass) and self.my_public_x == other.my_public_x + + +def sayHello(): # [invalid-name] + pass + + +class FooEnum(Enum): # [invalid-name] + const_with_snake_case = 42 + another_const = 43 + + +class Bar: # [invalid-name] + const_with_snake_case: ClassVar[int] = 42 diff --git a/tests/functional/n/name/name_preset_snake_case.rc b/tests/functional/n/name/name_preset_snake_case.rc new file mode 100644 index 000000000..2b04af5d4 --- /dev/null +++ b/tests/functional/n/name/name_preset_snake_case.rc @@ -0,0 +1,14 @@ +[BASIC] +function-naming-style=snake_case +const-naming-style=snake_case +attr-naming-style=snake_case +argument-naming-style=snake_case +module-naming-style=snake_case +method-naming-style=snake_case +variable-naming-style=snake_case +class-attribute-naming-style=snake_case +class-const-naming-style=snake_case +inlinevar-naming-style=snake_case +class-naming-style=snake_case + +include-naming-hint=yes diff --git a/tests/functional/n/name/name_preset_snake_case.txt b/tests/functional/n/name/name_preset_snake_case.txt new file mode 100644 index 000000000..c189797e9 --- /dev/null +++ b/tests/functional/n/name/name_preset_snake_case.txt @@ -0,0 +1,5 @@ +invalid-name:6:0::"Constant name ""SOME_CONSTANT"" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]*|__.*__)$' pattern)" +invalid-name:13:0:MyClass:"Class name ""MyClass"" doesn't conform to snake_case naming style ('[^\\W\\dA-Z][^\\WA-Z]+$' pattern)" +invalid-name:25:0:sayHello:"Function name ""sayHello"" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]{2,}|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)" +invalid-name:29:0:FooEnum:"Class name ""FooEnum"" doesn't conform to snake_case naming style ('[^\\W\\dA-Z][^\\WA-Z]+$' pattern)" +invalid-name:34:0:Bar:"Class name ""Bar"" doesn't conform to snake_case naming style ('[^\\W\\dA-Z][^\\WA-Z]+$' pattern)" diff --git a/tests/functional/n/name/name_styles.py b/tests/functional/n/name/name_styles.py new file mode 100644 index 000000000..394c82826 --- /dev/null +++ b/tests/functional/n/name/name_styles.py @@ -0,0 +1,156 @@ +"""Test for the invalid-name warning.""" +# pylint: disable=no-absolute-import, useless-object-inheritance, unnecessary-pass, unnecessary-comprehension +from __future__ import print_function +import abc +import collections +from enum import Enum +from typing import ClassVar + +GOOD_CONST_NAME = '' +bad_const_name = 0 # [invalid-name] + + +def BADFUNCTION_name(): # [invalid-name] + """Bad function name.""" + BAD_LOCAL_VAR = 1 # [invalid-name] + print(BAD_LOCAL_VAR) + + +def func_bad_argname(NOT_GOOD): # [invalid-name] + """Function with a badly named argument.""" + return NOT_GOOD + + +def no_nested_args(arg1, arg21, arg22): + """Well-formed function.""" + print(arg1, arg21, arg22) + + +class bad_class_name(object): # [invalid-name] + """Class with a bad name.""" + + +class CorrectClassName(object): + """Class with a good name.""" + + def __init__(self): + self._good_private_name = 10 + self.__good_real_private_name = 11 + self.good_attribute_name = 12 + self._Bad_AtTR_name = None # [invalid-name] + self.Bad_PUBLIC_name = None # [invalid-name] + + zz = 'Why Was It Bad Class Attribute?' + GOOD_CLASS_ATTR = 'Good Class Attribute' + + def BadMethodName(self): # [invalid-name] + """A Method with a bad name.""" + + def good_method_name(self): + """A method with a good name.""" + + def __DunDER_IS_not_free_for_all__(self): # [invalid-name] + """Another badly named method.""" + + +class DerivedFromCorrect(CorrectClassName): + """A derived class with an invalid inherited members. + + Derived attributes and methods with invalid names do not trigger warnings. + """ + zz = 'Now a good class attribute' + + def __init__(self): + super().__init__() + self._Bad_AtTR_name = None # Ignored + + def BadMethodName(self): + """Ignored since the method is in the interface.""" + + +V = [WHAT_Ever_inListComp for WHAT_Ever_inListComp in GOOD_CONST_NAME] + +def class_builder(): + """Function returning a class object.""" + + class EmbeddedClass(object): + """Useless class.""" + + return EmbeddedClass + +# +1:[invalid-name] +BAD_NAME_FOR_CLASS = collections.namedtuple('Named', ['tuple']) +NEXT_BAD_NAME_FOR_CLASS = class_builder() # [invalid-name] + +GoodName = collections.namedtuple('Named', ['tuple']) +ToplevelClass = class_builder() + +# Aliases for classes have the same name constraints. +AlsoCorrect = CorrectClassName +NOT_CORRECT = CorrectClassName # [invalid-name] + + +def test_globals(): + """Names in global statements are also checked.""" + global NOT_CORRECT + global AlsoCorrect # [invalid-name] + NOT_CORRECT = 1 + AlsoCorrect = 2 + + +class FooClass(object): + """A test case for property names. + + Since by default, the regex for attributes is the same as the one + for method names, we check the warning messages to contain the + string 'attribute'. + """ + @property + def PROPERTY_NAME(self): # [invalid-name] + """Ignored.""" + pass + + @abc.abstractproperty + def ABSTRACT_PROPERTY_NAME(self): # [invalid-name] + """Ignored.""" + pass + + @PROPERTY_NAME.setter + def PROPERTY_NAME_SETTER(self): # [invalid-name] + """Ignored.""" + pass + + def _nice_and_long_descriptive_private_method_name(self): + """private method with long name""" + pass + + +def good_public_function_name(good_arg_name): + """This is a perfect public function""" + good_variable_name = 1 + return good_variable_name + good_arg_name + + +def _private_scope_function_with_long_descriptive_name(): + """Private scope function are cool with long descriptive names""" + return 12 + +LONG_CONSTANT_NAME_IN_PUBLIC_SCOPE_ARE_OKAY = True +# We don't emit for non-const nodes +good_name_for_funcs = lambda: None +good_name_for_lists = [1, 2, 3] + +class _AnExceptionalExceptionThatOccursVeryVeryRarely(Exception): + """A very exceptional exception with a nice descriptive name""" + pass + +class FooEnum(Enum): + """A test case for enum names.""" + GOOD_ENUM_NAME = 1 + bad_enum_name = 2 # [invalid-name] + +class Bar: + """Class with class constants annotated with ClassVar.""" + CLASS_CONST: ClassVar[int] = 42 + CLASS_CONST2: ClassVar = "const" + variable: ClassVar[str] = "invalid name" # [invalid-name] diff --git a/tests/functional/n/name/name_styles.rc b/tests/functional/n/name/name_styles.rc new file mode 100644 index 000000000..1a63e671b --- /dev/null +++ b/tests/functional/n/name/name_styles.rc @@ -0,0 +1,5 @@ +[testoptions] +min_pyver=2.6 + +[Messages Control] +disable=too-few-public-methods,abstract-class-not-used,global-statement diff --git a/tests/functional/n/name/name_styles.txt b/tests/functional/n/name/name_styles.txt new file mode 100644 index 000000000..ef665c0de --- /dev/null +++ b/tests/functional/n/name/name_styles.txt @@ -0,0 +1,18 @@ +invalid-name:10:0::"Constant name ""bad_const_name"" doesn't conform to UPPER_CASE naming style" +invalid-name:13:0:BADFUNCTION_name:"Function name ""BADFUNCTION_name"" doesn't conform to snake_case naming style" +invalid-name:15:4:BADFUNCTION_name:"Variable name ""BAD_LOCAL_VAR"" doesn't conform to snake_case naming style" +invalid-name:19:0:func_bad_argname:"Argument name ""NOT_GOOD"" doesn't conform to snake_case naming style" +invalid-name:29:0:bad_class_name:"Class name ""bad_class_name"" doesn't conform to PascalCase naming style" +invalid-name:40:8:CorrectClassName.__init__:"Attribute name ""_Bad_AtTR_name"" doesn't conform to snake_case naming style" +invalid-name:41:8:CorrectClassName.__init__:"Attribute name ""Bad_PUBLIC_name"" doesn't conform to snake_case naming style" +invalid-name:46:4:CorrectClassName.BadMethodName:"Method name ""BadMethodName"" doesn't conform to snake_case naming style":INFERENCE +invalid-name:52:4:CorrectClassName.__DunDER_IS_not_free_for_all__:"Method name ""__DunDER_IS_not_free_for_all__"" doesn't conform to snake_case naming style":INFERENCE +invalid-name:82:0::"Class name ""BAD_NAME_FOR_CLASS"" doesn't conform to PascalCase naming style" +invalid-name:83:0::"Class name ""NEXT_BAD_NAME_FOR_CLASS"" doesn't conform to PascalCase naming style" +invalid-name:90:0::"Class name ""NOT_CORRECT"" doesn't conform to PascalCase naming style" +invalid-name:96:4:test_globals:"Constant name ""AlsoCorrect"" doesn't conform to UPPER_CASE naming style" +invalid-name:109:4:FooClass.PROPERTY_NAME:"Attribute name ""PROPERTY_NAME"" doesn't conform to snake_case naming style":INFERENCE +invalid-name:114:4:FooClass.ABSTRACT_PROPERTY_NAME:"Attribute name ""ABSTRACT_PROPERTY_NAME"" doesn't conform to snake_case naming style":INFERENCE +invalid-name:119:4:FooClass.PROPERTY_NAME_SETTER:"Attribute name ""PROPERTY_NAME_SETTER"" doesn't conform to snake_case naming style":INFERENCE +invalid-name:150:4:FooEnum:"Class constant name ""bad_enum_name"" doesn't conform to UPPER_CASE naming style" +invalid-name:156:4:Bar:"Class constant name ""variable"" doesn't conform to UPPER_CASE naming style" diff --git a/tests/functional/n/name/namedtuple_member_inference.py b/tests/functional/n/name/namedtuple_member_inference.py new file mode 100644 index 000000000..4488ceb0a --- /dev/null +++ b/tests/functional/n/name/namedtuple_member_inference.py @@ -0,0 +1,22 @@ +"""Test namedtuple attributes. + +Regression test for: +https://bitbucket.org/logilab/pylint/issue/93/pylint-crashes-on-namedtuple-attribute +""" +from __future__ import absolute_import, print_function +from collections import namedtuple + +__revision__ = None + +Thing = namedtuple('Thing', ()) + +Fantastic = namedtuple('Fantastic', ['foo']) + +def test(): + """Test member access in named tuples.""" + print(Thing.x) # [no-member] + fan = Fantastic(1) + print(fan.foo) + # Should not raise protected-access. + fan2 = fan._replace(foo=2) + print(fan2.foo) diff --git a/tests/functional/n/name/namedtuple_member_inference.txt b/tests/functional/n/name/namedtuple_member_inference.txt new file mode 100644 index 000000000..b754e9be7 --- /dev/null +++ b/tests/functional/n/name/namedtuple_member_inference.txt @@ -0,0 +1 @@ +no-member:17:10:test:Class 'Thing' has no 'x' member:INFERENCE diff --git a/tests/functional/n/name/names_in__all__.py b/tests/functional/n/name/names_in__all__.py new file mode 100644 index 000000000..9b2840acc --- /dev/null +++ b/tests/functional/n/name/names_in__all__.py @@ -0,0 +1,49 @@ +# pylint: disable=too-few-public-methods,no-self-use, no-absolute-import,import-error, useless-object-inheritance, unnecessary-pass +"""Test Pylint's use of __all__. + +* NonExistant is not defined in this module, and it is listed in + __all__. An error is expected. + +* This module imports path and republished it in __all__. No errors + are expected. +""" +from __future__ import print_function +from os import path +from collections import deque +from missing import Missing + +__all__ = [ + 'Dummy', + '', # [undefined-all-variable] + Missing, + SomeUndefined, # [undefined-variable] + 'NonExistant', # [undefined-all-variable] + 'path', + 'func', # [undefined-all-variable] + 'inner', # [undefined-all-variable] + 'InnerKlass', deque.__name__] # [undefined-all-variable] + + +class Dummy(object): + """A class defined in this module.""" + pass + +DUMMY = Dummy() + +def function(): + """Function docstring + """ + pass + +function() + +class Klass(object): + """A klass which contains a function""" + def func(self): + """A klass method""" + inner = None + print(inner) + + class InnerKlass(object): + """An inner klass""" + pass diff --git a/tests/functional/n/name/names_in__all__.txt b/tests/functional/n/name/names_in__all__.txt new file mode 100644 index 000000000..aa9015143 --- /dev/null +++ b/tests/functional/n/name/names_in__all__.txt @@ -0,0 +1,6 @@ +undefined-all-variable:17:4::Undefined variable name '' in __all__ +undefined-variable:19:4::Undefined variable 'SomeUndefined' +undefined-all-variable:20:4::Undefined variable name 'NonExistant' in __all__ +undefined-all-variable:22:4::Undefined variable name 'func' in __all__ +undefined-all-variable:23:4::Undefined variable name 'inner' in __all__ +undefined-all-variable:24:4::Undefined variable name 'InnerKlass' in __all__ |