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

Maint/typing #284

Merged
merged 54 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
6f62b67
Resolve typing issues
widerschein May 7, 2024
1710d1f
Add missing abstract property setter
widerschein May 7, 2024
959a390
Don't mark property as optional
widerschein May 7, 2024
f2781a0
Always expect a project UID
widerschein May 8, 2024
e85269c
Add missing method in base class
widerschein May 8, 2024
9b114a7
Fix rich comparison implementation
widerschein May 8, 2024
0e8daf7
Handle cases of no project loaded
widerschein May 8, 2024
f402da3
Add mypy config file
widerschein May 8, 2024
4caee90
Address typing issues for optiSLang server class properties
widerschein May 8, 2024
c1dae8b
Use plain string literals
widerschein May 8, 2024
8448663
Add type annotations
widerschein May 8, 2024
4221457
Reduce use of __class__ attribute in static methods
widerschein May 8, 2024
c054bcd
Reduce use of __class__
widerschein May 8, 2024
c1e7966
Fix missing return statement
widerschein May 8, 2024
c759b5b
Make Optional explicit
widerschein May 8, 2024
f699a80
Simplify test cases
widerschein May 10, 2024
0423eec
Call static methods with instance
widerschein May 10, 2024
0b610d6
Call static methods with instance
widerschein May 10, 2024
cfc3c9f
Fix class type annotations
widerschein May 10, 2024
fe757d7
Rename local
widerschein May 10, 2024
21bbea1
Ignore type
widerschein May 10, 2024
7f5d765
Use classmethod for factories
widerschein May 10, 2024
4499e98
Make helper function generic for Enum subclasses
widerschein May 10, 2024
fbd9ea0
Fix return type
widerschein May 10, 2024
d3f08f4
Avoid some awkward syntax
widerschein May 10, 2024
c868723
Add missing decorator
widerschein May 10, 2024
1cc1d86
Fix signature
widerschein May 10, 2024
abab2de
Fix narrowing for callable type
widerschein May 10, 2024
4c5c4a0
Don't use is_connected internally
widerschein May 10, 2024
a982382
Fix signature
widerschein May 10, 2024
2a2f7a4
Fix signature
widerschein May 10, 2024
86f6564
Fix more typing issues
widerschein May 10, 2024
752409d
Address type issues
widerschein May 12, 2024
67a85db
Address type issues
widerschein May 12, 2024
990eecf
Fix signature
widerschein May 12, 2024
0dca4d4
Fix parameter type
widerschein May 12, 2024
33a5ee3
Fix signatures
widerschein May 12, 2024
41a9941
Resolve more types issues
widerschein May 12, 2024
c8d81c1
Address issues and remove global constant
widerschein May 12, 2024
c84e237
Fix conditions
widerschein May 12, 2024
2ad2f36
Fix conditions
widerschein May 12, 2024
3c34bf1
Fix conditions
widerschein May 12, 2024
5202e3c
Add todos
widerschein May 12, 2024
e468904
Fix return types
widerschein May 12, 2024
61362b9
Remove duplicate method
widerschein May 12, 2024
b4b8317
Remove duplicate method
widerschein May 12, 2024
436b12a
Fix return value type
widerschein May 12, 2024
c87f7e1
Fix function calls
widerschein May 12, 2024
7ad6bbd
Tweak method
widerschein May 12, 2024
1cb707e
Remove global constant
widerschein May 12, 2024
a23ce19
Merge branch 'main' into maint/typing
widerschein May 13, 2024
b83795a
Adapt docstrings
widerschein May 13, 2024
2b64917
Adapt docstrings
widerschein May 13, 2024
66cc6dd
Merge branch 'main' into maint/typing
widerschein May 13, 2024
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
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[mypy]
mypy_path = $MYPY_CONFIG_FILE_DIR/src
explicit_package_bases = True
exclude = 00_run_script
2 changes: 0 additions & 2 deletions src/ansys/optislang/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@

__version__ = importlib.metadata.version(__name__.replace(".", "-"))

IRON_PYTHON = sys.platform == "cli"
PY3 = sys.version_info[0] >= 3
# First supported version of optiSLang: 2023R1
FIRST_SUPPORTED_VERSION = 231

Expand Down
10 changes: 5 additions & 5 deletions src/ansys/optislang/core/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

import sys

from ansys.optislang.core import IRON_PYTHON, PY3
from ansys.optislang.core import utils

defenc = sys.getdefaultencoding()

if PY3:
if sys.version_info[0] >= 3:
text_type = str
binary_type = bytes
else:
Expand Down Expand Up @@ -85,8 +85,8 @@ def force_bytes(text: str, encoding="ascii") -> bytes:
bytes
An encoded version of the string as a bytes object.
"""
if IRON_PYTHON:
return binary_type(text.encode(encoding, "ignore"), encoding, "ignore")
if utils.is_iron_python():
return binary_type(text.encode(encoding, "ignore"), encoding, "ignore") # type: ignore
else:
return text.encode(encoding, "ignore")

Expand All @@ -111,7 +111,7 @@ def force_text(data: bytes, encoding="utf-8") -> str:
if isinstance(data, string_types):
return data.decode(encoding, errors="ignore")

if PY3:
if sys.version_info[0] >= 3:
return text_type(data, encoding)
else:
return text_type(data)
6 changes: 3 additions & 3 deletions src/ansys/optislang/core/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ class RegisteredFileUsage(Enum):
INPUT_FILE = 2
OUTPUT_FILE = 3

@staticmethod
def from_str(string: str) -> RegisteredFileUsage:
@classmethod
def from_str(cls, string: str) -> RegisteredFileUsage:
"""Convert string to an instance of the ``RegisteredFileUsage`` class.

Parameters
Expand All @@ -294,7 +294,7 @@ def from_str(string: str) -> RegisteredFileUsage:
ValueError
Raised when an invalid value of ``string`` is given.
"""
return enum_from_str(string=string, enum_class=__class__, replace=(" ", "_"))
return enum_from_str(string=string, enum_class=cls, replace=(" ", "_"))

def to_str(self) -> str:
"""Convert usage type to string.
Expand Down
41 changes: 13 additions & 28 deletions src/ansys/optislang/core/node_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class AddinType(Enum):
PYTHON_BASED_MOP_NODE_PLUGIN = 5
PYTHON_BASED_NODE_PLUGIN = 6

@staticmethod
def from_str(string: str) -> AddinType:
@classmethod
def from_str(cls, string: str) -> AddinType:
"""Convert string to an instance of the ``AddinType`` class.

Parameters
Expand All @@ -63,7 +63,7 @@ def from_str(string: str) -> AddinType:
ValueError
Raised when an invalid value of ``string`` is given.
"""
return enum_from_str(string=string, enum_class=__class__, replace=(" ", "_"))
return enum_from_str(string=string, enum_class=cls, replace=(" ", "_"))


class NodeType:
Expand All @@ -89,26 +89,11 @@ def __str__(self):
"""Return formatted string."""
return f"type: {self.id}, subtype: {self.subtype}"

def __eq__(self, other: NodeType) -> bool:
"""Compare properties of two instances of the ``NodeType`` class.

Parameters
----------
other: NodeType
Criterion for comparison.

Returns
-------
bool
``True`` if all properties match; ``False`` otherwise.
"""
if type(self) == type(other):
checks = {}
checks["id"] = self.id == other.id
checks["subtype"] = self.subtype == other.subtype
return False not in checks.values()
else:
return False
def __eq__(self, other: object) -> bool:
"""Object comparison."""
if not isinstance(other, NodeType):
return NotImplemented
return self.id == other.id and self.subtype == other.subtype

@property
def id(self) -> str:
Expand Down Expand Up @@ -881,17 +866,17 @@ def get_node_type_from_str(node_id: str) -> NodeType:
if module_constants.get(node_id, None) is not None:
return module_constants[node_id]
elif node_id in customs.keys():
id = node_id[:-1]
id_ = node_id[:-1]
subtype = AddinType.BUILT_IN
else:
was_found = False
for custom in customs.keys():
if node_id.startswith(custom):
id = node_id.replace(custom, "")
subtype = customs[custom]
id_ = node_id.replace(custom, "")
subtype = customs[custom] # type: ignore
was_found = True
break
if not was_found:
id = node_id
id_ = node_id
subtype = AddinType.BUILT_IN
return NodeType(id=id, subtype=subtype)
return NodeType(id=id_, subtype=subtype)
51 changes: 26 additions & 25 deletions src/ansys/optislang/core/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class DesignFlow(Enum):
SEND = 2
RECEIVE_SEND = 3

@staticmethod
def from_str(string: str) -> DesignFlow:
@classmethod
def from_str(cls, string: str) -> DesignFlow:
"""Convert string to an instance of the ``DesignFlow`` class.

Parameters
Expand All @@ -85,7 +85,7 @@ def from_str(string: str) -> DesignFlow:
ValueError
Raised when an invalid value of ``string`` is given.
"""
return enum_from_str(string=string, enum_class=__class__, replace=(" ", "_"))
return enum_from_str(string=string, enum_class=cls, replace=(" ", "_"))


class NodeClassType(Enum):
Expand All @@ -97,8 +97,8 @@ class NodeClassType(Enum):
ROOT_SYSTEM = 3
INTEGRATION_NODE = 4

@staticmethod
def from_str(string: str) -> NodeClassType:
@classmethod
def from_str(cls, string: str) -> NodeClassType:
"""Convert string to an instance of the ``NodeClassType`` class.

Parameters
Expand All @@ -118,7 +118,7 @@ def from_str(string: str) -> NodeClassType:
ValueError
Raised when an invalid value of ``string`` is given.
"""
return enum_from_str(string=string, enum_class=__class__, replace=(" ", "_"))
return enum_from_str(string=string, enum_class=cls, replace=(" ", "_"))


class SamplingType(Enum):
Expand Down Expand Up @@ -148,8 +148,8 @@ class SamplingType(Enum):
FULLCOMBINATORIAL = 21
ADVANCEDLATINHYPER = 22

@staticmethod
def from_str(string: str) -> SamplingType:
@classmethod
def from_str(cls, string: str) -> SamplingType:
"""Convert string to an instance of the ``SamplingType`` class.

Parameters
Expand All @@ -167,7 +167,7 @@ def from_str(string: str) -> SamplingType:
TypeError
Raised when an invalid type of ``string`` is given.
"""
return enum_from_str(string=string, enum_class=__class__, replace=(" ", "_"))
return enum_from_str(string=string, enum_class=cls, replace=(" ", "_"))


class SlotType(Enum):
Expand All @@ -178,8 +178,8 @@ class SlotType(Enum):
INNER_INPUT = 2
INNER_OUTPUT = 3

@staticmethod
def from_str(string: str) -> SlotType:
@classmethod
def from_str(cls, string: str) -> SlotType:
"""Convert string to an instance of the ``SlotType`` class.

Parameters
Expand All @@ -197,7 +197,7 @@ def from_str(string: str) -> SlotType:
TypeError
Raised when an invalid type of ``string`` is given.
"""
return enum_from_str(string=string, enum_class=__class__, replace=(" ", "_"))
return enum_from_str(string=string, enum_class=cls, replace=(" ", "_"))

@staticmethod
def to_dir_str(type_: SlotType) -> str:
Expand Down Expand Up @@ -486,7 +486,7 @@ def get_parent_name(self) -> str: # pragma: no cover
pass

@abstractmethod
def get_property(self) -> Any: # pragma: no cover
def get_property(self, name: str) -> Any: # pragma: no cover
"""Get property from properties dictionary.

Parameters
Expand Down Expand Up @@ -663,13 +663,13 @@ def get_available_output_locations(self) -> Tuple: # pragma: no cover

@abstractmethod
def get_internal_variables(
self, include_reference_values: Optional[bool] = True
self, include_reference_values: bool = True
) -> Tuple: # pragma: no cover
"""Get internal variables.

Parameters
----------
include_reference_values: Optional[bool], optional
include_reference_values: bool
Whether reference values are to be included. By default ``True``.

Returns
Expand All @@ -690,13 +690,13 @@ def get_internal_variables(

@abstractmethod
def get_registered_input_slots(
self, include_reference_values: Optional[bool] = True
self, include_reference_values: bool = True
) -> Tuple: # pragma: no cover
"""Get registered input slots.

Parameters
----------
include_reference_values: Optional[bool], optional
include_reference_values: bool
Whether reference values are to be included. By default ``True``.

Returns
Expand All @@ -717,13 +717,13 @@ def get_registered_input_slots(

@abstractmethod
def get_registered_output_slots(
self, include_reference_values: Optional[bool] = True
self, include_reference_values: bool = True
) -> Tuple: # pragma: no cover
"""Get registered output slots.

Parameters
----------
include_reference_values: Optional[bool], optional
include_reference_values: bool
Whether reference values are to be included. By default ``True``.

Returns
Expand All @@ -744,13 +744,13 @@ def get_registered_output_slots(

@abstractmethod
def get_registered_parameters(
self, include_reference_values: Optional[bool] = True
self, include_reference_values: bool = True
) -> Tuple: # pragma: no cover
"""Get registered parameters.

Parameters
----------
include_reference_values: Optional[bool], optional
include_reference_values: bool
Whether reference values are to be included. By default ``True``.

Returns
Expand All @@ -771,13 +771,13 @@ def get_registered_parameters(

@abstractmethod
def get_registered_responses(
self, include_reference_values: Optional[bool] = True
self, include_reference_values: bool = True
) -> Tuple: # pragma: no cover
"""Get registered responses.

Parameters
----------
include_reference_values: Optional[bool], optional
include_reference_values: bool
Whether reference values are to be included. By default ``True``.

Returns
Expand Down Expand Up @@ -1261,6 +1261,7 @@ def get_inner_input_slots(self, name: Optional[str] = None) -> Tuple[InnerInputS
"""
pass

@abstractmethod
def get_inner_output_slots(self, name: Optional[str] = None) -> Tuple[InnerOutputSlot, ...]:
"""Get current node's inner output slots.

Expand Down Expand Up @@ -1361,7 +1362,7 @@ def __init__(self): # pragma: no cover
def control(
self,
command: str,
hid: Optional[str],
hid: Optional[str] = None,
wait_for_completion: bool = True,
timeout: Union[float, int] = 100,
) -> Optional[bool]: # pragma: no cover
Expand Down Expand Up @@ -1543,7 +1544,7 @@ def type_hint(self) -> Optional[str]: # pragma: no cover
pass

@abstractmethod
def get_connections(self) -> Tuple[Edge]: # pragma: no cover
def get_connections(self) -> Tuple[Edge, ...]: # pragma: no cover
"""Get connections for the current slot.

Returns
Expand Down
Loading
Loading