agronholm/typeguard

View on GitHub
src/typeguard/_transformer.py

Summary

Maintainability
F
1 wk
Test Coverage

File _transformer.py has 981 lines of code (exceeds 250 allowed). Consider refactoring.
Open

from __future__ import annotations

import ast
import builtins
import sys
Severity: Major
Found in src/typeguard/_transformer.py - About 2 days to fix

    Function visit_FunctionDef has a Cognitive Complexity of 117 (exceeds 5 allowed). Consider refactoring.
    Open

        def visit_FunctionDef(
            self, node: FunctionDef | AsyncFunctionDef
        ) -> FunctionDef | AsyncFunctionDef | None:
            """
            Injects type checks for function arguments, and for a return of None if the
    Severity: Minor
    Found in src/typeguard/_transformer.py - About 2 days to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function visit_Assign has a Cognitive Complexity of 43 (exceeds 5 allowed). Consider refactoring.
    Open

        def visit_Assign(self, node: Assign) -> Any:
            """
            This injects a type check into a local variable assignment within a function
            body. The variable must have been annotated earlier in the function body.
    
    
    Severity: Minor
    Found in src/typeguard/_transformer.py - About 6 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function _use_memo has a Cognitive Complexity of 40 (exceeds 5 allowed). Consider refactoring.
    Open

        def _use_memo(
            self, node: ClassDef | FunctionDef | AsyncFunctionDef
        ) -> Generator[None, Any, None]:
            new_memo = TransformMemo(node, self._memo, self._memo.path + (node.name,))
            old_memo = self._memo
    Severity: Minor
    Found in src/typeguard/_transformer.py - About 6 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function visit_Subscript has a Cognitive Complexity of 35 (exceeds 5 allowed). Consider refactoring.
    Open

        def visit_Subscript(self, node: Subscript) -> Any:
            if self._memo.is_ignored_name(node.value):
                return None
    
            # The subscript of typing(_extensions).Literal can be any arbitrary string, so
    Severity: Minor
    Found in src/typeguard/_transformer.py - About 5 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    TypeguardTransformer has 21 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class TypeguardTransformer(NodeTransformer):
        def __init__(
            self, target_path: Sequence[str] | None = None, target_lineno: int | None = None
        ) -> None:
            self._target_path = tuple(target_path) if target_path else None
    Severity: Minor
    Found in src/typeguard/_transformer.py - About 2 hrs to fix

      Function __post_init__ has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
      Open

          def __post_init__(self) -> None:
              elements: list[str] = []
              memo = self
              while isinstance(memo.node, (ClassDef, FunctionDef, AsyncFunctionDef)):
                  elements.insert(0, memo.node.name)
      Severity: Minor
      Found in src/typeguard/_transformer.py - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function name_matches has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
      Open

          def name_matches(self, expression: expr | Expr | None, *names: str) -> bool:
              if expression is None:
                  return False
      
              path: list[str] = []
      Severity: Minor
      Found in src/typeguard/_transformer.py - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function visit_BinOp has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
      Open

          def visit_BinOp(self, node: BinOp) -> Any:
              self.generic_visit(node)
      
              if isinstance(node.op, BitOr):
                  # If either branch of the BinOp has been transformed to `None`, it means
      Severity: Minor
      Found in src/typeguard/_transformer.py - About 55 mins to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Avoid deeply nested control flow statements.
      Open

                              if sys.version_info < (3, 9):
                                  subscript_slice = Index(subscript_slice, ctx=Load())
      
      
      Severity: Major
      Found in src/typeguard/_transformer.py - About 45 mins to fix

        Avoid deeply nested control flow statements.
        Open

                                if sys.version_info < (3, 9):
                                    subscript_slice = Index(subscript_slice, ctx=Load())
        
        
        Severity: Major
        Found in src/typeguard/_transformer.py - About 45 mins to fix

          Avoid deeply nested control flow statements.
          Open

                                  if len(items) > 1:
                                      new_memo.send_annotation = self._convert_annotation(
                                          items[1]
                                      )
          
          
          Severity: Major
          Found in src/typeguard/_transformer.py - About 45 mins to fix

            Avoid deeply nested control flow statements.
            Open

                                    if len(items) > 2:
                                        new_memo.return_annotation = self._convert_annotation(
                                            items[2]
                                        )
                            else:
            Severity: Major
            Found in src/typeguard/_transformer.py - About 45 mins to fix

              Avoid deeply nested control flow statements.
              Open

                                      if node.args.args:
                                          if node.name == "__new__":
                                              memo_kwargs["self_type"] = Name(
                                                  id=node.args.args[0].arg, ctx=Load()
                                              )
              Severity: Major
              Found in src/typeguard/_transformer.py - About 45 mins to fix

                Avoid deeply nested control flow statements.
                Open

                                        if isinstance(decorator, Call) and decorator.keywords:
                                            self._memo.configuration_overrides = {
                                                kw.arg: kw.value for kw in decorator.keywords if kw.arg
                                            }
                
                
                Severity: Major
                Found in src/typeguard/_transformer.py - About 45 mins to fix

                  Avoid deeply nested control flow statements.
                  Open

                                          if (
                                              isinstance(decorator, Name)
                                              and decorator.id == "staticmethod"
                                          ):
                                              break
                  Severity: Major
                  Found in src/typeguard/_transformer.py - About 45 mins to fix

                    Avoid deeply nested control flow statements.
                    Open

                                            if expression is None:
                                                item[key] = self._get_import("typing", "Any")
                    
                    
                    Severity: Major
                    Found in src/typeguard/_transformer.py - About 45 mins to fix

                      Avoid deeply nested control flow statements.
                      Open

                                              if isinstance(annotation_slice, Index):
                                                  annotation_slice = (
                                                      annotation_slice.value  # type: ignore[attr-defined]
                                                  )
                      
                      
                      Severity: Major
                      Found in src/typeguard/_transformer.py - About 45 mins to fix

                        Function is_ignored_name has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                        Open

                            def is_ignored_name(self, expression: expr | Expr | None) -> bool:
                                top_expression = (
                                    expression.value if isinstance(expression, Expr) else expression
                                )
                        
                        
                        Severity: Minor
                        Found in src/typeguard/_transformer.py - About 45 mins to fix

                        Cognitive Complexity

                        Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                        A method's cognitive complexity is based on a few simple rules:

                        • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                        • Code is considered more complex for each "break in the linear flow of the code"
                        • Code is considered more complex when "flow breaking structures are nested"

                        Further reading

                        Avoid deeply nested control flow statements.
                        Open

                                                if len(items) > 0:
                                                    new_memo.yield_annotation = self._convert_annotation(
                                                        items[0]
                                                    )
                        
                        
                        Severity: Major
                        Found in src/typeguard/_transformer.py - About 45 mins to fix

                          Avoid deeply nested control flow statements.
                          Open

                                                  if sys.version_info >= (3, 9):
                                                      container = Name("tuple", ctx=Load())
                                                  else:
                                                      container = self._get_import("typing", "Tuple")
                          
                          
                          Severity: Major
                          Found in src/typeguard/_transformer.py - About 45 mins to fix

                            Avoid deeply nested control flow statements.
                            Open

                                                    if annotation:
                                                        annotations_[Constant(name)] = annotation
                                                        check_required = True
                                                    else:
                                                        annotations_[Constant(name)] = None
                            Severity: Major
                            Found in src/typeguard/_transformer.py - About 45 mins to fix

                              Function visit_ClassDef has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                              Open

                                  def visit_ClassDef(self, node: ClassDef) -> ClassDef | None:
                                      self._memo.local_names.add(node.name)
                              
                                      # Eliminate top level classes not belonging to the target path
                                      if (
                              Severity: Minor
                              Found in src/typeguard/_transformer.py - About 45 mins to fix

                              Cognitive Complexity

                              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                              A method's cognitive complexity is based on a few simple rules:

                              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                              • Code is considered more complex for each "break in the linear flow of the code"
                              • Code is considered more complex when "flow breaking structures are nested"

                              Further reading

                              Avoid deeply nested control flow statements.
                              Open

                                                      if sys.version_info >= (3, 9):
                                                          container = Name("dict", ctx=Load())
                                                      else:
                                                          container = self._get_import("typing", "Dict")
                              
                              
                              Severity: Major
                              Found in src/typeguard/_transformer.py - About 45 mins to fix

                                Avoid deeply nested control flow statements.
                                Open

                                                        if isinstance(annotation_slice, Tuple):
                                                            items = annotation_slice.elts
                                                        else:
                                                            items = [annotation_slice]
                                
                                
                                Severity: Major
                                Found in src/typeguard/_transformer.py - About 45 mins to fix

                                  Consider simplifying this complex logical expression.
                                  Open

                                              if (
                                                  self._memo.return_annotation
                                                  and (not self._memo.is_async or not self._memo.has_yield_expressions)
                                                  and not isinstance(node.body[-1], Return)
                                                  and (
                                  Severity: Major
                                  Found in src/typeguard/_transformer.py - About 40 mins to fix

                                    Function visit_AnnAssign has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                                    Open

                                        def visit_AnnAssign(self, node: AnnAssign) -> Any:
                                            """
                                            This injects a type check into a local variable annotation-assignment within a
                                            function body.
                                    
                                    
                                    Severity: Minor
                                    Found in src/typeguard/_transformer.py - About 35 mins to fix

                                    Cognitive Complexity

                                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                    A method's cognitive complexity is based on a few simple rules:

                                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                    • Code is considered more complex for each "break in the linear flow of the code"
                                    • Code is considered more complex when "flow breaking structures are nested"

                                    Further reading

                                    Function visit_Name has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                                    Open

                                        def visit_Name(self, node: Name) -> Any:
                                            if self._memo.is_ignored_name(node):
                                                return None
                                    
                                            if sys.version_info < (3, 9):
                                    Severity: Minor
                                    Found in src/typeguard/_transformer.py - About 35 mins to fix

                                    Cognitive Complexity

                                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                    A method's cognitive complexity is based on a few simple rules:

                                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                    • Code is considered more complex for each "break in the linear flow of the code"
                                    • Code is considered more complex when "flow breaking structures are nested"

                                    Further reading

                                    Avoid too many return statements within this function.
                                    Open

                                            return node
                                    Severity: Major
                                    Found in src/typeguard/_transformer.py - About 30 mins to fix

                                      Avoid too many return statements within this function.
                                      Open

                                                          return node.value
                                      Severity: Major
                                      Found in src/typeguard/_transformer.py - About 30 mins to fix

                                        Avoid too many return statements within this function.
                                        Open

                                                return node
                                        Severity: Major
                                        Found in src/typeguard/_transformer.py - About 30 mins to fix

                                          Avoid too many return statements within this function.
                                          Open

                                                              return node.value
                                          Severity: Major
                                          Found in src/typeguard/_transformer.py - About 30 mins to fix

                                            Avoid too many return statements within this function.
                                            Open

                                                        return False
                                            Severity: Major
                                            Found in src/typeguard/_transformer.py - About 30 mins to fix

                                              There are no issues that match your filters.

                                              Category
                                              Status