Ejemplo n.º 1
0
    def _ensure_underscores(self, node: ast.AST, name: str):
        if access.is_private(name):
            self._error_callback(
                naming.PrivateNameViolation(node, text=name),
            )

        if logical.does_contain_underscored_number(name):
            self._error_callback(
                naming.UnderscoredNumberNameViolation(node, text=name),
            )

        if logical.does_contain_consecutive_underscores(name):
            self._error_callback(
                naming.ConsecutiveUnderscoresInNameViolation(
                    node, text=name,
                ),
            )

        if builtins.is_wrong_alias(name):
            self._error_callback(
                naming.TrailingUnderscoreViolation(node, text=name),
            )

        if access.is_unused(name) and len(name) > 1:
            self._error_callback(
                naming.WrongUnusedVariableNameViolation(node, text=name),
            )
Ejemplo n.º 2
0
def does_contain_consecutive_underscores(name: str) -> bool:
    """
    Checks if name contains consecutive underscores in middle of name.

    >>> does_contain_consecutive_underscores('name')
    False

    >>> does_contain_consecutive_underscores('__magic__')
    False

    >>> does_contain_consecutive_underscores('__private')
    False

    >>> does_contain_consecutive_underscores('name')
    False

    >>> does_contain_consecutive_underscores('some__value')
    True

    >>> does_contain_consecutive_underscores('__some__value__')
    True

    >>> does_contain_consecutive_underscores('__private__value')
    True

    >>> does_contain_consecutive_underscores('some_value__')
    True

    """
    if access.is_magic(name) or access.is_private(name):
        return '__' in name.strip('_')
    return '__' in name
Ejemplo n.º 3
0
 def _ideal_order(self, first: str) -> int:
     if first == '__new__':
         return 4  # highest priority
     if first == '__init__':
         return 3
     if access.is_protected(first):
         return 1
     if access.is_private(first):
         return 0  # lowest priority
     return 2  # public and magic methods
Ejemplo n.º 4
0
    def _ideal_order(self, first: str) -> int:
        base_methods_order = {
            '__new__': 5,  # highest priority
            '__init__': 4,
            '__call__': 3,
        }
        public_and_magic_methods_priority = 2

        if access.is_protected(first):
            return 1
        if access.is_private(first):
            return 0  # lowest priority
        return base_methods_order.get(first, public_and_magic_methods_priority)
Ejemplo n.º 5
0
    def _check_module_name(self) -> None:
        if logical.is_wrong_name(self.stem, constants.MODULE_NAMES_BLACKLIST):
            self.add_violation(naming.WrongModuleNameViolation())

        if access.is_magic(self.stem):
            if self.stem not in constants.MAGIC_MODULE_NAMES_WHITELIST:
                self.add_violation(naming.WrongModuleMagicNameViolation())

        if access.is_private(self.stem):
            self.add_violation(naming.PrivateNameViolation(text=self.stem))

        if logical.does_contain_unicode(self.stem):
            self.add_violation(naming.UnicodeNameViolation(text=self.stem))