Skip to content

Commit

Permalink
CU-86dtb6tu5 - Change the usage of typing.Union and `typing.Optiona…
Browse files Browse the repository at this point in the history
…l` to use the native `|` operator and update tests accordingly
  • Loading branch information
luc10921 committed May 7, 2024
1 parent ae2d62c commit 2909845
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 37 deletions.
16 changes: 12 additions & 4 deletions boa3/internal/analyser/typeanalyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self, analyser, symbol_table: dict[str, ISymbol], log: bool = False
self.modules: dict[str, Module] = {}
self.symbols: dict[str, ISymbol] = symbol_table

self._is_annotation: bool = False
self._current_class: UserClass = None
self._current_method: Method = None
self._scope_stack: list[SymbolScope] = []
Expand Down Expand Up @@ -257,8 +258,10 @@ def visit_arg(self, arg: ast.arg):
CompilerError.TypeHintMissing(arg.lineno, arg.col_offset, symbol_id=arg.arg)
)

self._is_annotation = True
# continue to walk through the tree
self.generic_visit(arg)
self._is_annotation = False

def visit_Return(self, ret: ast.Return):
"""
Expand Down Expand Up @@ -891,10 +894,15 @@ def visit_BinOp(self, bin_op: ast.BinOp) -> IType | None:
:return: the type of the result of the operation if the operation is valid. Otherwise, returns None
:rtype: IType or None
"""
operation = self.validate_binary_operation(bin_op, bin_op.left, bin_op.right)
if operation is not None:
bin_op.op = operation
return operation.result
if self._is_annotation and isinstance(bin_op.op, ast.BitOr):
left_type = self.get_type(bin_op.left)
right_type = self.get_type(bin_op.right)
return left_type.union_type(right_type)
else:
operation = self.validate_binary_operation(bin_op, bin_op.left, bin_op.right)
if operation is not None:
bin_op.op = operation
return operation.result

def validate_binary_operation(self, node: ast.AST, left_op: ast.AST, right_op: ast.AST) -> IOperation | None:
"""
Expand Down
2 changes: 0 additions & 2 deletions boa3/internal/model/operation/binary/additional/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
__all__ = ['CollectionMembership',
'CollectionNotMembership',
'UnionTypesOperation'
]

from boa3.internal.model.operation.binary.additional.membership import CollectionMembership
from boa3.internal.model.operation.binary.additional.notmembership import CollectionNotMembership
from boa3.internal.model.operation.binary.additional.uniontypesoperation import UnionTypesOperation

This file was deleted.

1 change: 0 additions & 1 deletion boa3/internal/model/operation/binaryop.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class BinaryOp:
# Other operations
In = CollectionMembership()
NotIn = CollectionNotMembership()
UnionTypesOp = UnionTypesOperation()

@classmethod
def validate_type(cls, operator: Operator, left: IType, right: IType) -> BinaryOperation | None:
Expand Down

0 comments on commit 2909845

Please sign in to comment.