Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

address some mypy warnings #1814

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion slither/core/cfg/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def set_function(self, function: "Function") -> None:
self._function = function

@property
def function(self) -> "Function":
def function(self) -> Optional["Function"]:
return self._function

# endregion
Expand Down
4 changes: 2 additions & 2 deletions slither/core/compilation_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ def add_modifier(self, modif: Modifier) -> None:
self._all_modifiers.add(modif)

@property
def functions_and_modifiers(self) -> List[Function]:
return self.functions + list(self.modifiers)
def functions_and_modifiers(self) -> List[Union[Function, Modifier]]:
return self.functions + self.modifiers

def propagate_function_calls(self) -> None:
for f in self.functions_and_modifiers:
Expand Down
11 changes: 6 additions & 5 deletions slither/core/expressions/literal.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from typing import Optional, Union, TYPE_CHECKING, Any
from typing import Optional, Union, Any, TypeVar

from slither.core.expressions.expression import Expression
from slither.core.solidity_types.elementary_type import Fixed, Int, Ufixed, Uint
from slither.utils.arithmetic import convert_subdenomination
from slither.utils.integer_conversion import convert_string_to_int

if TYPE_CHECKING:
from slither.core.solidity_types.type import Type
from slither.core.solidity_types.type import Type

TypeT = TypeVar("TypeT", bound=Type)


class Literal(Expression):
def __init__(
self, value: Union[int, str], custom_type: "Type", subdenomination: Optional[str] = None
self, value: Union[int, str], custom_type: TypeT, subdenomination: Optional[str] = None
) -> None:
super().__init__()
self._value = value
Expand All @@ -30,7 +31,7 @@ def converted_value(self) -> Union[int, str]:
return self._value

@property
def type(self) -> "Type":
def type(self) -> TypeT:
return self._type

@property
Expand Down
4 changes: 2 additions & 2 deletions slither/core/slither_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ def parse_ignore_comments(self, file: str) -> None:
if ignored:
for check in ignored:
vals = self._ignore_ranges[file][check]
if len(vals) == 0 or vals[-1][1] != float("inf"):
if len(vals) == 0 or vals[-1][1] != -1:
# First item in the array, or the prior item is fully populated.
self._ignore_ranges[file][check].append((line_number, float("inf")))
self._ignore_ranges[file][check].append((line_number, -1))
else:
logger.error(
f"Consecutive slither-disable-starts without slither-disable-end in {file}#{line_number}"
Expand Down
4 changes: 2 additions & 2 deletions slither/core/variables/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ def visibility(self) -> Optional[str]:
def visibility(self, v: str) -> None:
self._visibility = v

def set_type(self, t: Optional[Union[List, Type, str]]) -> None:
def set_type(self, t: Optional[Union[List[Type], Type, str]]) -> None:
if isinstance(t, str):
self._type = ElementaryType(t)
return
assert isinstance(t, (Type, list)) or t is None
assert isinstance(t, (Type, List[Type])) or t is None
self._type = t

@property
Expand Down
16 changes: 13 additions & 3 deletions slither/slithir/operations/lvalue.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Any, List, Optional
from typing import Any, List, Optional, TypeVar

from slither.core.variables import Variable
from slither.slithir.operations.operation import Operation

VariableT = TypeVar("VariableT", bound=Variable)


class OperationWithLValue(Operation):
"""
Expand All @@ -12,12 +14,20 @@ class OperationWithLValue(Operation):
def __init__(self) -> None:
super().__init__()

self._lvalue: Optional[Variable] = None
self._lvalue: VariableT

@property
def lvalue(self) -> Optional[Variable]:
def lvalue(self) -> VariableT:
return self._lvalue

@lvalue.setter
def lvalue(self, lvalue: VariableT) -> None:
self._lvalue = lvalue

@property
def used(self):
return self.read + [self.lvalue]

@lvalue.setter
def lvalue(self, lvalue: Variable) -> None:
self._lvalue = lvalue
Expand Down
2 changes: 1 addition & 1 deletion slither/solc_parsing/variables/event_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from slither.core.variables.event_variable import EventVariable


class EventVariableSolc(VariableDeclarationSolc):
class EventVariableSolc(VariableDeclarationSolc[EventVariable]):
def __init__(self, variable: EventVariable, variable_data: Dict):
super().__init__(variable, variable_data)

Expand Down
2 changes: 1 addition & 1 deletion slither/solc_parsing/variables/function_type_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from slither.core.variables.function_type_variable import FunctionTypeVariable


class FunctionTypeVariableSolc(VariableDeclarationSolc):
class FunctionTypeVariableSolc(VariableDeclarationSolc[FunctionTypeVariable]):
def __init__(self, variable: FunctionTypeVariable, variable_data: Dict):
super().__init__(variable, variable_data)

Expand Down
2 changes: 1 addition & 1 deletion slither/solc_parsing/variables/local_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from slither.core.variables.local_variable import LocalVariable


class LocalVariableSolc(VariableDeclarationSolc):
class LocalVariableSolc(VariableDeclarationSolc[LocalVariable]):
def __init__(self, variable: LocalVariable, variable_data: Dict) -> None:
super().__init__(variable, variable_data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from slither.core.variables.local_variable_init_from_tuple import LocalVariableInitFromTuple


class LocalVariableInitFromTupleSolc(VariableDeclarationSolc):
class LocalVariableInitFromTupleSolc(VariableDeclarationSolc[LocalVariableInitFromTuple]):
def __init__(
self, variable: LocalVariableInitFromTuple, variable_data: Dict, index: int
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion slither/solc_parsing/variables/state_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from slither.core.variables.state_variable import StateVariable


class StateVariableSolc(VariableDeclarationSolc):
class StateVariableSolc(VariableDeclarationSolc[StateVariable]):
def __init__(self, variable: StateVariable, variable_data: Dict) -> None:
super().__init__(variable, variable_data)

Expand Down
2 changes: 1 addition & 1 deletion slither/solc_parsing/variables/structure_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from slither.core.variables.structure_variable import StructureVariable


class StructureVariableSolc(VariableDeclarationSolc):
class StructureVariableSolc(VariableDeclarationSolc[StructureVariable]):
def __init__(self, variable: StructureVariable, variable_data: Dict):
super().__init__(variable, variable_data)

Expand Down
2 changes: 1 addition & 1 deletion slither/solc_parsing/variables/top_level_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from slither.core.compilation_unit import SlitherCompilationUnit


class TopLevelVariableSolc(VariableDeclarationSolc, CallerContextExpression):
class TopLevelVariableSolc(VariableDeclarationSolc[TopLevelVariable], CallerContextExpression):
def __init__(
self,
variable: TopLevelVariable,
Expand Down
9 changes: 6 additions & 3 deletions slither/solc_parsing/variables/variable_declaration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import re
from typing import Dict, Optional, Union
from typing import Dict, Optional, Union, TypeVar, Generic

from slither.solc_parsing.declarations.caller_context import CallerContextExpression
from slither.solc_parsing.expressions.expression_parsing import parse_expression
Expand Down Expand Up @@ -29,9 +29,12 @@ class MultipleVariablesDeclaration(Exception):
pass


class VariableDeclarationSolc:
T = TypeVar("T", bound=Variable)


class VariableDeclarationSolc(Generic[T]):
# pylint: disable=too-many-branches
def __init__(self, variable: Variable, variable_data: Dict) -> None:
def __init__(self, variable: T, variable_data: Dict) -> None:
"""
A variable can be declared through a statement, or directly.
If it is through a statement, the following children may contain
Expand Down
4 changes: 2 additions & 2 deletions slither/tools/doctor/checks/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from typing import Optional, Any
import urllib

from packaging.version import parse, Version
from packaging.version import parse, Version, LegacyVersion

from slither.utils.colors import yellow, green


def get_installed_version(name: str) -> Optional[Version]:
def get_installed_version(name: str) -> Optional[Version | LegacyVersion]:
try:
return parse(metadata.version(name))
except metadata.PackageNotFoundError:
Expand Down
12 changes: 6 additions & 6 deletions slither/utils/erc.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ def erc_to_signatures(erc: List[ERC]):
# https://eips.ethereum.org/EIPS/eip-2612
# Must have ERC20

ERC2612_EVENTS = []
ERC2612 = [
ERC2612_EVENTS: List[ERC_EVENT] = []
ERC2612: List[ERC] = [
ERC(
"permit",
["address", "address", "uint256", "uint256", "uint8", "bytes32", "bytes32"],
Expand All @@ -344,8 +344,8 @@ def erc_to_signatures(erc: List[ERC]):
# https://eips.ethereum.org/EIPS/eip-1363
# Must have ERC20 and ERC165

ERC1363_EVENTS = []
ERC1363 = (
ERC1363_EVENTS: List[ERC_EVENT] = []
ERC1363: List[ERC] = (
[
ERC("transferAndCall", ["address", "uint256"], "bool", False, True, []),
ERC("transferAndCall", ["address", "uint256", "bytes"], "bool", False, True, []),
Expand All @@ -371,8 +371,8 @@ def erc_to_signatures(erc: List[ERC]):
# https://eips.ethereum.org/EIPS/eip-4524
# Must have ERC20 and ERC165

ERC4524_EVENTS = []
ERC4524 = (
ERC4524_EVENTS: List[ERC_EVENT] = []
ERC4524: List[ERC] = (
[
ERC("safeTransfer", ["address", "uint256"], "bool", False, True, []),
ERC("safeTransfer", ["address", "uint256", "bytes"], "bool", False, True, []),
Expand Down
6 changes: 3 additions & 3 deletions slither/utils/integer_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def convert_string_to_fraction(val: Union[str, bytes, int]) -> Fraction:
val = val.replace("_", "")

if "e" in val or "E" in val:
base, expo = val.split("e") if "e" in val else val.split("E")
base, expo = Fraction(base), int(expo)
parsed_base, parsed_expo = val.split("e") if "e" in val else val.split("E")
base, expo = Fraction(parsed_base), int(parsed_expo)
# The resulting number must be < 2**256-1, otherwise solc
# Would not be able to compile it
# 10**77 is the largest exponent that fits
Expand All @@ -27,7 +27,7 @@ def convert_string_to_fraction(val: Union[str, bytes, int]) -> Fraction:
raise SlitherError(
f"{base}e{expo} is too large to fit in any Solidity integer size"
)
return 0
return Fraction(0, 1)
return Fraction(base) * Fraction(10**expo)

return Fraction(val)
Expand Down
Loading