diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c65044a25b..fdb781b3b5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,7 +10,7 @@ repos: - id: end-of-file-fixer exclude: tests/testdata - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.7.4" + rev: "v0.8.0" hooks: - id: ruff args: ["--fix"] diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index c44687bf89..57cb56e914 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -358,12 +358,16 @@ def _looks_like_special_alias(node: Call) -> bool: PY39: Callable = _CallableType(collections.abc.Callable, 2) """ return isinstance(node.func, Name) and ( - node.func.name == "_TupleType" - and isinstance(node.args[0], Name) - and node.args[0].name == "tuple" - or node.func.name == "_CallableType" - and isinstance(node.args[0], Attribute) - and node.args[0].as_string() == "collections.abc.Callable" + ( + node.func.name == "_TupleType" + and isinstance(node.args[0], Name) + and node.args[0].name == "tuple" + ) + or ( + node.func.name == "_CallableType" + and isinstance(node.args[0], Attribute) + and node.args[0].as_string() == "collections.abc.Callable" + ) ) @@ -400,11 +404,8 @@ def infer_special_alias( def _looks_like_typing_cast(node: Call) -> bool: - return ( - isinstance(node.func, Name) - and node.func.name == "cast" - or isinstance(node.func, Attribute) - and node.func.attrname == "cast" + return (isinstance(node.func, Name) and node.func.name == "cast") or ( + isinstance(node.func, Attribute) and node.func.attrname == "cast" ) diff --git a/astroid/context.py b/astroid/context.py index d5efc9a278..777565aeaa 100644 --- a/astroid/context.py +++ b/astroid/context.py @@ -37,13 +37,13 @@ class InferenceContext: """ __slots__ = ( - "path", - "lookupname", - "callcontext", + "_nodes_inferred", "boundnode", - "extra_context", + "callcontext", "constraints", - "_nodes_inferred", + "extra_context", + "lookupname", + "path", ) max_inferred = 100 @@ -164,7 +164,7 @@ def __str__(self) -> str: class CallContext: """Holds information for a call site.""" - __slots__ = ("args", "keywords", "callee") + __slots__ = ("args", "callee", "keywords") def __init__( self, diff --git a/astroid/decorators.py b/astroid/decorators.py index cd0f5a7961..613118ae79 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -147,11 +147,13 @@ def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R: # - len(args) needs to be long enough, if too short # arg can't be in args either # - args[index] should not be None - or arg not in kwargs - and ( - index == -1 - or len(args) <= index - or (len(args) > index and args[index] is None) + or ( + arg not in kwargs + and ( + index == -1 + or len(args) <= index + or (len(args) > index and args[index] is None) + ) ) ): warnings.warn( diff --git a/astroid/helpers.py b/astroid/helpers.py index ea7523b94f..62a72595e0 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -300,9 +300,8 @@ def object_len(node, context: InferenceContext | None = None): and result_of_len.pytype() == "builtins.int" ): return result_of_len.value - if ( - result_of_len is None - or isinstance(result_of_len, bases.Instance) + if result_of_len is None or ( + isinstance(result_of_len, bases.Instance) and result_of_len.is_subtype_of("builtins.int") ): # Fake a result as we don't know the arguments of the instance call. diff --git a/astroid/nodes/__init__.py b/astroid/nodes/__init__.py index d34a54292f..7202385c33 100644 --- a/astroid/nodes/__init__.py +++ b/astroid/nodes/__init__.py @@ -202,8 +202,9 @@ ) __all__ = ( + "CONST_CLS", + "SYNTHETIC_ROOT", "AnnAssign", - "are_exclusive", "Arguments", "Assert", "Assign", @@ -219,20 +220,17 @@ "BinOp", "BoolOp", "Break", - "builtin_lookup", "Call", "ClassDef", - "CONST_CLS", "Compare", "Comprehension", "ComprehensionScope", "Const", - "const_factory", "Continue", "Decorators", "DelAttr", - "Delete", "DelName", + "Delete", "Dict", "DictComp", "DictUnpack", @@ -243,9 +241,7 @@ "For", "FormattedValue", "FunctionDef", - "function_to_method", "GeneratorExp", - "get_wrapping_class", "Global", "If", "IfExp", @@ -277,7 +273,6 @@ "Position", "Raise", "Return", - "SYNTHETIC_ROOT", "Set", "SetComp", "Slice", @@ -291,9 +286,14 @@ "TypeVarTuple", "UnaryOp", "Unknown", - "unpack_infer", "While", "With", "Yield", "YieldFrom", + "are_exclusive", + "builtin_lookup", + "const_factory", + "function_to_method", + "get_wrapping_class", + "unpack_infer", ) diff --git a/astroid/nodes/_base_nodes.py b/astroid/nodes/_base_nodes.py index 65b703d042..ca9fcd70a6 100644 --- a/astroid/nodes/_base_nodes.py +++ b/astroid/nodes/_base_nodes.py @@ -609,13 +609,11 @@ def _get_binop_flow( and op == "|" and ( isinstance(left, (bases.UnionType, nodes.ClassDef)) - or isinstance(left, nodes.Const) - and left.value is None + or (isinstance(left, nodes.Const) and left.value is None) ) and ( isinstance(right, (bases.UnionType, nodes.ClassDef)) - or isinstance(right, nodes.Const) - and right.value is None + or (isinstance(right, nodes.Const) and right.value is None) ) ): methods.extend([partial(OperatorNode._bin_op_or_union_type, left, right)]) diff --git a/astroid/nodes/as_string.py b/astroid/nodes/as_string.py index d063350b4c..ceb74108d1 100644 --- a/astroid/nodes/as_string.py +++ b/astroid/nodes/as_string.py @@ -571,7 +571,7 @@ def visit_while(self, node: nodes.While) -> str: def visit_with(self, node: nodes.With) -> str: # 'with' without 'as' is possible """return an astroid.With node as string""" items = ", ".join( - f"{expr.accept(self)}" + (v and f" as {v.accept(self)}" or "") + f"{expr.accept(self)}" + ((v and f" as {v.accept(self)}") or "") for expr, v in node.items ) return f"with {items}:\n{self._stmt_list(node.body)}" diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py index 4f845bf7b9..e1b8ae2738 100644 --- a/astroid/nodes/node_classes.py +++ b/astroid/nodes/node_classes.py @@ -4791,10 +4791,8 @@ def _infer_with_values( ) -> Generator[InferenceResult, None, InferenceErrorInfo | None]: uninferable_already_generated = False for inferred in self._infer_from_values(self.values, context): - failed = ( - inferred is util.Uninferable - or isinstance(inferred, Const) - and UNINFERABLE_VALUE in inferred.value + failed = inferred is util.Uninferable or ( + isinstance(inferred, Const) and UNINFERABLE_VALUE in inferred.value ) if failed: if not uninferable_already_generated: diff --git a/astroid/nodes/scoped_nodes/__init__.py b/astroid/nodes/scoped_nodes/__init__.py index 92a0f9a95a..01f99fa3d5 100644 --- a/astroid/nodes/scoped_nodes/__init__.py +++ b/astroid/nodes/scoped_nodes/__init__.py @@ -28,6 +28,7 @@ from astroid.nodes.scoped_nodes.utils import builtin_lookup __all__ = ( + "SYNTHETIC_ROOT", "AsyncFunctionDef", "ClassDef", "ComprehensionScope", @@ -38,10 +39,9 @@ "ListComp", "LocalsDictNodeNG", "Module", - "SYNTHETIC_ROOT", "SetComp", + "_is_metaclass", "builtin_lookup", "function_to_method", "get_wrapping_class", - "_is_metaclass", ) diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py index 99ed79675b..d4a348caab 100644 --- a/astroid/nodes/scoped_nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes/scoped_nodes.py @@ -2128,7 +2128,7 @@ def scope_lookup( ) if ( any( - node == base or base.parent_of(node) and not self.type_params + node == base or (base.parent_of(node) and not self.type_params) for base in self.bases ) or lookup_upper_frame diff --git a/script/test_bump_changelog.py b/script/test_bump_changelog.py index a04701b76f..70c896c96b 100644 --- a/script/test_bump_changelog.py +++ b/script/test_bump_changelog.py @@ -50,10 +50,8 @@ ) def test_get_next_version(version, version_type, expected_version, expected_versions): assert get_next_version(version, version_type) == expected_version - if ( - version_type == VersionType.PATCH - or version_type == VersionType.MINOR - and version.endswith(".0") + if version_type == VersionType.PATCH or ( + version_type == VersionType.MINOR and version.endswith(".0") ): assert get_next_versions(version, version_type) == expected_versions