Skip to content

Commit

Permalink
Upgrade to latest mypy (#618)
Browse files Browse the repository at this point in the history
* upgrade to latest mypy

PEP-484 has changed to recommend against implicit optionals based on default arguments, and mypy has accordingly flipped the default in a recent release.

ref: https://peps.python.org/pep-0484/#union-types

* remove specifying locations for mypy to check in Makefile in favour of mypy.ini

also, --show-error-codes is default, ref: https://mypy.readthedocs.io/en/stable/error_codes.html#displaying-error-codes

* consistently use `| None` for all methods touched by this PR #618
  • Loading branch information
achidlow committed Dec 20, 2022
1 parent 6c1adac commit 6f28661
Show file tree
Hide file tree
Showing 27 changed files with 79 additions and 71 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ black:
flake8:
flake8 $(ALLPY)

MYPY = pyteal scripts tests
mypy:
mypy --show-error-codes $(MYPY)
mypy

lint: black flake8 mypy

Expand Down
2 changes: 2 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[mypy]
files = pyteal,scripts,tests
python_version = 3.10

[mypy-semantic_version.*]
ignore_missing_imports = True
Expand Down
6 changes: 3 additions & 3 deletions pyteal/ast/abi/array_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def decode(
self,
encoded: Expr,
*,
start_index: Expr = None,
end_index: Expr = None,
length: Expr = None
start_index: Expr | None = None,
end_index: Expr | None = None,
length: Expr | None = None
) -> Expr:
"""Decode a substring of the passed in encoded byte string and set it as this type's value.
Expand Down
6 changes: 3 additions & 3 deletions pyteal/ast/abi/bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ def decode(
self,
encoded: Expr,
*,
start_index: Expr = None,
end_index: Expr = None,
length: Expr = None
start_index: Expr | None = None,
end_index: Expr | None = None,
length: Expr | None = None
) -> Expr:
if start_index is None:
start_index = Int(0)
Expand Down
6 changes: 3 additions & 3 deletions pyteal/ast/abi/reference_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def decode(
self,
encoded: Expr,
*,
start_index: Expr = None,
end_index: Expr = None,
length: Expr = None,
start_index: Expr | None = None,
end_index: Expr | None = None,
length: Expr | None = None,
) -> Expr:
return uint_decode(
self.type_spec().bit_size(),
Expand Down
8 changes: 4 additions & 4 deletions pyteal/ast/abi/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __str__(self) -> str:


class Transaction(BaseType):
def __init__(self, spec: TransactionTypeSpec = None) -> None:
def __init__(self, spec: TransactionTypeSpec | None = None) -> None:
if spec is None:
super().__init__(TransactionTypeSpec())
else:
Expand Down Expand Up @@ -97,9 +97,9 @@ def decode(
self,
encoded: Expr,
*,
start_index: Expr = None,
end_index: Expr = None,
length: Expr = None,
start_index: Expr | None = None,
end_index: Expr | None = None,
length: Expr | None = None,
) -> Expr:
raise TealInputError("A Transaction cannot be decoded")

Expand Down
6 changes: 3 additions & 3 deletions pyteal/ast/abi/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ def decode(
self,
encoded: Expr,
*,
start_index: Expr = None,
end_index: Expr = None,
length: Expr = None,
start_index: Expr | None = None,
end_index: Expr | None = None,
length: Expr | None = None,
) -> Expr:
extracted = substring_for_decoding(
encoded, start_index=start_index, end_index=end_index, length=length
Expand Down
6 changes: 3 additions & 3 deletions pyteal/ast/abi/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ def decode(
self,
encoded: Expr,
*,
start_index: Expr = None,
end_index: Expr = None,
length: Expr = None,
start_index: Expr | None = None,
end_index: Expr | None = None,
length: Expr | None = None,
) -> Expr:
"""Decode a substring of the passed in encoded string and set it as this type's value.
Expand Down
6 changes: 3 additions & 3 deletions pyteal/ast/abi/uint.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ def decode(
self,
encoded: Expr,
*,
start_index: Expr = None,
end_index: Expr = None,
length: Expr = None,
start_index: Expr | None = None,
end_index: Expr | None = None,
length: Expr | None = None,
) -> Expr:
return uint_decode(
self.type_spec().bit_size(),
Expand Down
6 changes: 3 additions & 3 deletions pyteal/ast/abi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
def substring_for_decoding(
encoded: Expr,
*,
start_index: Expr = None,
end_index: Expr = None,
length: Expr = None,
start_index: Expr | None = None,
end_index: Expr | None = None,
length: Expr | None = None,
) -> Expr:
"""A helper function for getting the substring to decode according to the rules of BaseType.decode."""
if length is not None and end_index is not None:
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/acct_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_acct_param_version_checks(method_name, field_name):

account_param_field = AccountParamField[field_name]

def test_unsupported_version(version: int, match: str = None):
def test_unsupported_version(version: int, match: str | None = None):
with pytest.raises(pt.TealInputError, match=match):
unsupported_options_version = pt.CompileOptions(version=version)
expr.__teal__(unsupported_options_version)
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/assert_.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Assert(Expr):
"""A control flow expression to verify that a condition is true."""

def __init__(
self, cond: Expr, *additional_conds: Expr, comment: str = None
self, cond: Expr, *additional_conds: Expr, comment: str | None = None
) -> None:
"""Create an assert statement that raises an error if the condition is false.
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, arg1: str | bytes | bytearray) -> None:
def __init__(self, arg1: str, arg2: str) -> None:
pass

def __init__(self, arg1: str | bytes | bytearray, arg2: str = None) -> None:
def __init__(self, arg1: str | bytes | bytearray, arg2: str | None = None) -> None:
"""
__init__(arg1: Union[str, bytes, bytearray]) -> None
__init__(self, arg1: str, arg2: str) -> None
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def has_return(self):
CommentExpr.__module__ = "pyteal"


def Comment(comment: str, expr: Expr = None) -> Expr:
def Comment(comment: str, expr: Expr | None = None) -> Expr:
"""Wrap an existing expression with a comment.
This comment will be present in the compiled TEAL source immediately before the first op of the
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/if_.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class If(Expr):
"""Simple two-way conditional expression."""

def __init__(
self, cond: Expr, thenBranch: Expr = None, elseBranch: Expr = None
self, cond: Expr, thenBranch: Expr | None = None, elseBranch: Expr | None = None
) -> None:
"""Create a new If expression.
Expand Down
6 changes: 3 additions & 3 deletions pyteal/ast/maybe.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def __init__(
op: Op,
type: TealType,
*,
immediate_args: List[Union[int, str]] = None,
args: List[Expr] = None,
compile_check: Callable[["CompileOptions"], None] = None,
immediate_args: List[Union[int, str]] | None = None,
args: List[Expr] | None = None,
compile_check: Callable[["CompileOptions"], None] | None = None,
):
"""Create a new MaybeValue.
Expand Down
4 changes: 2 additions & 2 deletions pyteal/ast/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def __init__(
op: Op,
types: List[TealType],
*,
immediate_args: List[Union[int, str]] = None,
args: List[Expr] = None,
immediate_args: List[Union[int, str]] | None = None,
args: List[Expr] | None = None,
compile_check: Callable[["CompileOptions"], None] = lambda _: None,
):
"""Create a new MultiValue.
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/opup.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class OpUp:
)
"""

def __init__(self, mode: OpUpMode, target_app_id: Expr = None):
def __init__(self, mode: OpUpMode, target_app_id: Expr | None = None):
"""Create a new OpUp object.
Args:
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/return_.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class Return(Expr):
"""Return a value from the current execution context."""

def __init__(self, value: Expr = None) -> None:
def __init__(self, value: Expr | None = None) -> None:
"""Create a new Return expression.
If called from the main program, this will immediately exit the program
Expand Down
28 changes: 14 additions & 14 deletions pyteal/ast/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ class Router:
def __init__(
self,
name: str,
bare_calls: BareCallActions = None,
descr: str = None,
bare_calls: BareCallActions | None = None,
descr: str | None = None,
) -> None:
"""
Args:
Expand Down Expand Up @@ -547,9 +547,9 @@ def __init__(
def add_method_handler(
self,
method_call: ABIReturnSubroutine,
overriding_name: str = None,
method_config: MethodConfig = None,
description: str = None,
overriding_name: str | None = None,
method_config: MethodConfig | None = None,
description: str | None = None,
) -> ABIReturnSubroutine:
"""Add a method call handler to this Router.
Expand Down Expand Up @@ -605,17 +605,17 @@ def add_method_handler(

def method(
self,
func: Callable = None,
func: Callable | None = None,
/,
*,
name: str = None,
description: str = None,
no_op: CallConfig = None,
opt_in: CallConfig = None,
close_out: CallConfig = None,
clear_state: CallConfig = None,
update_application: CallConfig = None,
delete_application: CallConfig = None,
name: str | None = None,
description: str | None = None,
no_op: CallConfig | None = None,
opt_in: CallConfig | None = None,
close_out: CallConfig | None = None,
clear_state: CallConfig | None = None,
update_application: CallConfig | None = None,
delete_application: CallConfig | None = None,
):
"""This is an alternative way to register a method, as supposed to :code:`add_method_handler`.
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/router_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def multiple_txn(
]


def power_set(no_dup_list: list, length_override: int = None):
def power_set(no_dup_list: list, length_override: int | None = None):
"""
This function serves as a generator for all possible elements in power_set
over `non_dup_list`, which is a list of non-duplicated elements (matches property of a set).
Expand Down
15 changes: 9 additions & 6 deletions pyteal/ast/scratch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import cast, TYPE_CHECKING, Optional
from typing import cast, TYPE_CHECKING

from pyteal.types import TealType, require_type
from pyteal.config import NUM_SLOTS
Expand All @@ -17,7 +17,7 @@ class ScratchSlot:
# Slot ids under 256 are manually reserved slots
nextSlotId = NUM_SLOTS

def __init__(self, requestedSlotId: int = None):
def __init__(self, requestedSlotId: int | None = None):
"""Initializes a scratch slot with a particular id
Args:
Expand All @@ -38,7 +38,7 @@ def __init__(self, requestedSlotId: int = None):
self.id = requestedSlotId
self.isReservedSlot = True

def store(self, value: Expr = None) -> Expr:
def store(self, value: Expr | None = None) -> Expr:
"""Get an expression to store a value in this slot.
Args:
Expand Down Expand Up @@ -102,9 +102,9 @@ class ScratchLoad(Expr):

def __init__(
self,
slot: ScratchSlot = None,
slot: ScratchSlot | None = None,
type: TealType = TealType.anytype,
index_expression: Expr = None,
index_expression: Expr | None = None,
):
"""Create a new ScratchLoad expression.
Expand Down Expand Up @@ -168,7 +168,10 @@ class ScratchStore(Expr):
"""Expression to store a value in scratch space."""

def __init__(
self, slot: Optional[ScratchSlot], value: Expr, index_expression: Expr = None
self,
slot: ScratchSlot | None,
value: Expr,
index_expression: Expr | None = None,
):
"""Create a new ScratchStore expression.
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/scratchvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ScratchVar(AbstractVar):
])
"""

def __init__(self, type: TealType = TealType.anytype, slotId: int = None):
def __init__(self, type: TealType = TealType.anytype, slotId: int | None = None):
"""Create a new ScratchVar with an optional type.
Args:
Expand Down
6 changes: 3 additions & 3 deletions pyteal/ast/subroutine.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init__(
self.__name: str = name_str if name_str else self.implementation.__name__

def _validate(
self, input_types: list[TealType | None] = None
self, input_types: list[TealType | None] | None = None
) -> tuple[
MappingProxyType[str, Parameter],
dict[str, type],
Expand Down Expand Up @@ -433,7 +433,7 @@ def __init__(
subroutine: SubroutineDefinition,
args: list[Expr | ScratchVar | abi.BaseType],
*,
output_kwarg: OutputKwArgInfo = None,
output_kwarg: OutputKwArgInfo | None = None,
) -> None:
super().__init__()
self.subroutine = subroutine
Expand Down Expand Up @@ -674,7 +674,7 @@ def __call__(
def name(self) -> str:
return self.subroutine.name()

def method_signature(self, overriding_name: str = None) -> str:
def method_signature(self, overriding_name: str | None = None) -> str:
if not self.is_abi_routable():
raise TealInputError(
"Only registrable methods may return a method signature"
Expand Down
12 changes: 8 additions & 4 deletions pyteal/ir/tealblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def isTerminal(self) -> bool:
return len(self.getOutgoing()) == 0

def validateTree(
self, parent: "TealBlock" = None, visited: List["TealBlock"] = None
self,
parent: "TealBlock | None" = None,
visited: List["TealBlock"] | None = None,
) -> None:
"""Check that this block and its children have valid parent pointers.
Expand All @@ -62,7 +64,9 @@ def validateTree(
block.validateTree(self, visited)

def addIncoming(
self, parent: "TealBlock" = None, visited: List["TealBlock"] = None
self,
parent: "TealBlock | None" = None,
visited: List["TealBlock"] | None = None,
) -> None:
"""Calculate the parent blocks for this block and its children.
Expand All @@ -85,8 +89,8 @@ def addIncoming(

def validateSlots(
self,
slotsInUse: Set["ScratchSlot"] = None,
visited: Set[Tuple[int, ...]] = None,
slotsInUse: Set["ScratchSlot"] | None = None,
visited: Set[Tuple[int, ...]] | None = None,
) -> List[TealCompileError]:
if visited is None:
visited = set()
Expand Down

0 comments on commit 6f28661

Please sign in to comment.