Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
style: apply automated linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
megalinter-bot committed May 10, 2024
1 parent c379d65 commit 2c58b77
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,14 @@ def _handle_unknown_call(self, call: Symbol, reason: Reasons) -> None:
# Deal with the case that the call calls a function parameter.
elif isinstance(call, Parameter):
self.call_graph_forest.get_graph(reason.id).reasons.unknown_calls[call.id] = UnknownProto(
symbol=call, origin=reason.function_scope.symbol if reason.function_scope else None,
symbol=call,
origin=reason.function_scope.symbol if reason.function_scope else None,
)

else:
self.call_graph_forest.get_graph(reason.id).reasons.unknown_calls[call.id] = UnknownProto(
symbol=call, origin=reason.function_scope.symbol if reason.function_scope else None,
symbol=call,
origin=reason.function_scope.symbol if reason.function_scope else None,
)

def _handle_cycles(self, removed_nodes: set[NodeID] | None = None) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
ImpurityReason,
NativeCall,
NodeID,
NonLocalVariableRead,
NonLocalVariableWrite,
OpenMode,
PackageData,
Parameter,
Expand Down Expand Up @@ -240,11 +238,15 @@ def _get_impurity_result(reasons: Reasons) -> PurityResult:
# If the inferred node is a function, it must be analyzed to determine its purity.
if isinstance(read.symbol.inferred_node, astroid.FunctionDef):
impurity_reasons.add(
UnknownCall(UnknownFunctionCall(call=read.symbol.call, inferred_def=read.symbol.inferred_node)),
UnknownCall(
UnknownFunctionCall(call=read.symbol.call, inferred_def=read.symbol.inferred_node),
),
)
elif isinstance(read.symbol.inferred_node, astroid.ClassDef):
impurity_reasons.add(
UnknownCall(UnknownClassInit(call=read.symbol.call, inferred_def=read.symbol.inferred_node)),
UnknownCall(
UnknownClassInit(call=read.symbol.call, inferred_def=read.symbol.inferred_node),
),
)
# If the inferred node is a module, it will not count towards the impurity of the function.
# If this was added, nearly anything would be impure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@
MemberAccessValue,
ModuleAnalysisResult,
NodeID,
NonLocalVariableRead,
NonLocalVariableWrite,
PackageData,
ParameterKind,
Reasons,
Reference,
ReferenceNode,
Symbol,
TargetReference,
ValueReference,
NonLocalVariableWrite,
NonLocalVariableRead,
UnknownProto,
ValueReference,
)

_BUILTINS = dir(builtins)
Expand Down Expand Up @@ -755,7 +755,8 @@ def _resolve_references(self) -> tuple[dict[str, list[ReferenceNode]], dict[Node
# of the raw_reasons dict for this function
elif call_references_result.node not in raw_reasons[function.symbol.id].unknown_calls:
raw_reasons[function.symbol.id].unknown_calls[call_references_result.node.id] = (
UnknownProto(symbol=call_references_result.node, origin=function.symbol))
UnknownProto(symbol=call_references_result.node, origin=function.symbol)
)

# Check if the function has value_references (References from a value node to a target node).
if function.value_references:
Expand Down Expand Up @@ -785,7 +786,8 @@ def _resolve_references(self) -> tuple[dict[str, list[ReferenceNode]], dict[Node
# Add the referenced symbol to the list of symbols whom are read from.
if referenced_symbol.id not in raw_reasons[function.symbol.id].reads_from:
raw_reasons[function.symbol.id].reads_from[referenced_symbol.id] = (
NonLocalVariableRead(symbol=referenced_symbol, origin=function.symbol))
NonLocalVariableRead(symbol=referenced_symbol, origin=function.symbol)
)
elif isinstance(referenced_symbol, Import):
# Since calls of imported functions are treated within _find_value_references
# as MemberAccessValue, they need to be added to the calls of the raw_reasons dict
Expand All @@ -799,14 +801,18 @@ def _resolve_references(self) -> tuple[dict[str, list[ReferenceNode]], dict[Node
else: # noqa: PLR5501
if referenced_symbol.id not in raw_reasons[function.symbol.id].reads_from:
raw_reasons[function.symbol.id].reads_from[referenced_symbol.id] = (
NonLocalVariableRead(symbol=referenced_symbol, origin=function.symbol))
NonLocalVariableRead(
symbol=referenced_symbol, origin=function.symbol,
)
)
# If no referenced symbols are found, add the call to the list of unknown_calls
# of the raw_reasons dict for this function
elif value_reference_result.node not in raw_reasons[
function.symbol.id
].unknown_calls and isinstance(value_reference_result.node.node, astroid.Call):
raw_reasons[function.symbol.id].unknown_calls[value_reference_result.node.id] = (
UnknownProto(symbol=value_reference_result.node, origin=function.symbol))
UnknownProto(symbol=value_reference_result.node, origin=function.symbol)
)

# Check if the function has target_references (References from a target node to another target node).
if function.target_symbols:
Expand Down Expand Up @@ -840,7 +846,8 @@ def _resolve_references(self) -> tuple[dict[str, list[ReferenceNode]], dict[Node
# Add the referenced symbol to the list of symbols whom are written to.
if referenced_symbol not in raw_reasons[function.symbol.id].writes_to:
raw_reasons[function.symbol.id].writes_to[referenced_symbol.id] = (
NonLocalVariableWrite(symbol=referenced_symbol, origin=function.symbol))
NonLocalVariableWrite(symbol=referenced_symbol, origin=function.symbol)
)

name_references: dict[str, list[ReferenceNode]] = self.merge_dicts(value_references, target_references)
resolved_references: dict[str, list[ReferenceNode]] = self.merge_dicts(call_references, name_references)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from dataclasses import dataclass, field
from typing import TYPE_CHECKING

from library_analyzer.processing.api.purity_analysis.model._purity import Impure, UnknownCall, UnknownFunctionCall

if TYPE_CHECKING:
from library_analyzer.processing.api.purity_analysis.model._module_data import (
Import,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from library_analyzer.processing.api.purity_analysis.model._module_data import (
MemberAccessValue,
NodeID,
Reference,
Symbol,
UnknownSymbol,
Reference,
)
from library_analyzer.utils import ensure_file_exists

Expand Down Expand Up @@ -224,9 +224,7 @@ def to_dict(self, shorten: bool = False) -> dict[str, Any]:
}
return {
"purity": self.__class__.__name__,
"reasons": {
reason: value for reason, value in combined_reasons.items() if value
},
"reasons": {reason: value for reason, value in combined_reasons.items() if value},
}

def __hash__(self) -> int:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@

from library_analyzer.processing.api.purity_analysis.model._module_data import (
ClassScope,
ClassVariable,
FunctionScope,
GlobalVariable,
Import,
InstanceVariable,
MemberAccessTarget,
MemberAccessValue,
NodeID,
Expand All @@ -22,14 +18,14 @@
)

if TYPE_CHECKING:
from collections.abc import Iterator

from library_analyzer.processing.api.purity_analysis.model import (
CallGraphForest,
PurityResult,
NonLocalVariableRead,
NonLocalVariableWrite,
UnknownProto)
PurityResult,
UnknownProto,
)


@dataclass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ def transform_reasons(reasons: dict[NodeID, Reasons]) -> dict[str, SimpleReasons
{
(
f"{target_reference.symbol.__class__.__name__}.{target_reference.symbol.klass.name}.{target_reference.symbol.node.name}.line{target_reference.symbol.node.fromlineno}" # type: ignore[union-attr] # "None" has no attribute "name" but since we check for the type before, this is fine
if isinstance(target_reference.symbol, ClassVariable) and target_reference.symbol.klass is not None
if isinstance(target_reference.symbol, ClassVariable)
and target_reference.symbol.klass is not None
else (
f"{target_reference.symbol.__class__.__name__}.{target_reference.symbol.klass.name}.{target_reference.symbol.node.member}.line{target_reference.symbol.node.node.fromlineno}" # type: ignore[union-attr] # "None" has no attribute "name" but since we check for the type before, this is fine
if isinstance(target_reference.symbol, InstanceVariable)
Expand Down

0 comments on commit 2c58b77

Please sign in to comment.