Skip to content

Commit 1205059

Browse files
committed
feat: Added type hints
1 parent 79460c3 commit 1205059

12 files changed

Lines changed: 147 additions & 75 deletions

File tree

pyproject.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "scrap_engine"
77
authors = [{ name = "lxgr", email = "lxgr@protonmail.com" }]
88
description = "A 2D ascii game engine for the terminal"
99
keywords = ["game", "Ascii"]
10-
version = "1.5.4"
10+
version = "1.5.4-rc1"
1111
license = { text = "GPL-3.0-only" }
1212
readme = "README.md"
1313
requires-python = ">=3.12"
@@ -21,6 +21,16 @@ Homepage = "https://github.com/lxgr-linux/scrap_engine"
2121
Issues = "https://github.com/lxgr-linux/scrap_engine/issue"
2222
Repository = "https://github.com/lxgr-linux/scrap_engine.git"
2323

24+
[tool.basedpyright]
25+
reportMissingTypeStubs = false
26+
reportUnknownMemberType = false
27+
reportUnannotatedClassAttribute = false
28+
deprecateTypingAliases = false
29+
reportImplicitStringConcatenation = false
30+
executionEnvironments = [
31+
{ root = "src" },
32+
]
33+
2434
[tool.ruff]
2535
line-length = 88
2636
target-version = "py312"

src/scrap_engine/addable/misc/box.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from typing import override
2+
13
from scrap_engine.addable.addable import Addable
24
from scrap_engine.addable.area import HasArea
35
from scrap_engine.addable.object_group import ObjectGroup
46
from scrap_engine.addable.state import DEFAULT_STATE
7+
from scrap_engine.map import Map
58

69

710
class Box(ObjectGroup[Addable], HasArea):
@@ -16,14 +19,17 @@ def __init__(self, height: int, width: int):
1619
self.__width: int = width
1720

1821
@property
22+
@override
1923
def height(self) -> int:
2024
return self.__height
2125

2226
@property
27+
@override
2328
def width(self) -> int:
2429
return self.__width
2530

26-
def add(self, _map, x: int, y: int):
31+
@override
32+
def add(self, _map: Map, x: int, y: int):
2733
"""
2834
Adds the box to a certain coordinate on a certain map.
2935
"""
@@ -34,6 +40,7 @@ def add(self, _map, x: int, y: int):
3440
obj.add(self.map, obj.rx + self.x, obj.ry + self.y)
3541
self.added = True
3642

43+
@override
3744
def add_ob(self, obj: Addable, x: int, y: int):
3845
"""
3946
Adds an object(group) to a certain coordinate relative to the box.
@@ -56,6 +63,7 @@ def set_ob(self, obj: Addable, x: int, y: int):
5663
if self.added:
5764
obj.set(obj.rx + self.x, obj.ry + self.y)
5865

66+
@override
5967
def remove(self):
6068
"""
6169
Removes the box from the map.

src/scrap_engine/addable/misc/circle.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import math
2-
from typing import Type
2+
from typing import Type, override
33

44
from scrap_engine.addable.state import DEFAULT_STATE, State
55

6-
from ..object import Object
6+
from ..object import ArgProto, Object
77
from .box import Box
88

99

@@ -18,18 +18,18 @@ def __init__(
1818
radius: int,
1919
state: State = DEFAULT_STATE,
2020
ob_class: Type[Object] = Object,
21-
ob_args=None,
21+
ob_args: ArgProto = None,
2222
):
2323
super().__init__(0, 0)
2424
if ob_args is None:
2525
ob_args = {}
26-
self.char = char
26+
self.char: str = char
2727
self.ob_class = ob_class
2828
self.ob_args = ob_args
2929
self.state = state
3030
self.__gen(radius)
3131

32-
def __gen(self, radius):
32+
def __gen(self, radius: int):
3333
self.radius = radius
3434
for i in range(-(int(radius) + 1), int(radius + 1) + 1):
3535
for j in range(-(int(radius) + 1), int(radius + 1) + 1):
@@ -42,15 +42,16 @@ def __gen(self, radius):
4242
j,
4343
)
4444

45-
def rechar(self, char):
45+
def rechar(self, char: str):
4646
"""
4747
Changes the chars the circle is filled with.
4848
"""
4949
self.char = char
5050
for obj in self.obs:
5151
obj.rechar(char)
5252

53-
def resize(self, radius):
53+
@override
54+
def resize(self, radius: int):
5455
"""
5556
Resizes the circle.
5657
"""

src/scrap_engine/addable/misc/frame.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typing import Optional, Type
1+
from typing import Optional, Type, override
22

33
from scrap_engine.addable.state import DEFAULT_STATE, State
44

5-
from ..object import Object
5+
from ..object import ArgProto, Object
66
from .box import Box
77
from .square import Square
88

@@ -28,7 +28,7 @@ def __init__(
2828
vertical_chars: Optional[list[str]] = None,
2929
state: State = DEFAULT_STATE,
3030
ob_class: Type[Object] = Object,
31-
ob_args=None,
31+
ob_args: ArgProto = None,
3232
):
3333
super().__init__(height, width)
3434
if ob_args is None:
@@ -83,13 +83,16 @@ def __gen_obs(self):
8383
ob_class=Object,
8484
ob_args=self.ob_args,
8585
)
86-
for i, j in zip(self.vertical_chars, range(2))
86+
for i, _ in zip(self.vertical_chars, range(2))
8787
]
8888
for obj, rx, ry in zip(self.verticals, [0, self.width - 1], [1, 1]):
8989
self.add_ob(obj, rx, ry)
9090

9191
def rechar(
92-
self, corner_chars=None, horizontal_chars=None, vertical_chars=None
92+
self,
93+
corner_chars: Optional[list[str]] = None,
94+
horizontal_chars: Optional[list[str]] = None,
95+
vertical_chars: Optional[list[str]] = None,
9396
):
9497
"""
9598
Rechars the frame.
@@ -108,7 +111,8 @@ def rechar(
108111
for obj, _c in zip(self.verticals, self.vertical_chars):
109112
obj.rechar(_c)
110113

111-
def resize(self, height, width):
114+
@override
115+
def resize(self, height: int, width: int):
112116
"""
113117
Changes the frames size.
114118
"""

src/scrap_engine/addable/misc/line.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from scrap_engine.addable.state import DEFAULT_STATE, State
55

6-
from ..object import Object
6+
from ..object import ArgProto, Object
77
from .box import Box
88

99
LineType = Literal["straight", "crippled"]
@@ -22,7 +22,7 @@ def __init__(
2222
l_type: LineType = "straight",
2323
state: State = DEFAULT_STATE,
2424
ob_class: Type[Object] = Object,
25-
ob_args=None,
25+
ob_args: ArgProto = None,
2626
):
2727
super().__init__(0, 0)
2828
if ob_args is None:

src/scrap_engine/addable/misc/square.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from scrap_engine.addable.state import DEFAULT_STATE, State
44

5-
from ..object import Object
5+
from ..object import ArgProto, Object
66
from .box import Box
77

88

@@ -13,12 +13,12 @@ class Square(Box):
1313

1414
def __init__(
1515
self,
16-
char,
17-
width,
18-
height,
16+
char: str,
17+
width: int,
18+
height: int,
1919
state: State = DEFAULT_STATE,
2020
ob_class: Type[Object] = Object,
21-
ob_args=None,
21+
ob_args: ArgProto = None,
2222
):
2323
super().__init__(height, width)
2424
if ob_args is None:
@@ -34,9 +34,7 @@ def __create(self):
3434
for ry in range(self.height):
3535
for rx in range(self.width):
3636
self.add_ob(
37-
self.ob_class(
38-
self.char, self.state, arg_proto=self.ob_args
39-
),
37+
self.ob_class(self.char, self.state, arg_proto=self.ob_args),
4038
rx,
4139
ry,
4240
)

src/scrap_engine/addable/misc/text.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
from typing import Generic, TypeVar
1+
from typing import Any, Generic, TypeVar, override
22

33
from scrap_engine.addable.area import HasArea
44
from scrap_engine.addable.state import DEFAULT_STATE, State
5+
from scrap_engine.map import Map
56

6-
from ..object import Object
7+
from ..object import ArgProto, Object
78
from ..object_group import ObjectGroup
89

910
T = TypeVar("T", bound=Object)
1011

1112

12-
class Text(HasArea, ObjectGroup[T], Generic[T]):
13+
class Text(ObjectGroup[T], HasArea, Generic[T]):
1314
"""
1415
A datatype containing a string, that can be added to a map.
1516
Different Texts can be added together with the '+' operator.
1617
"""
1718

1819
def __init__(
1920
self,
20-
text,
21+
text: str,
2122
state: State = DEFAULT_STATE,
22-
esccode="",
23+
esccode: str = "",
2324
ob_class: type[T] = Object,
24-
ob_args=None,
25-
ignore="",
25+
ob_args: ArgProto = None,
26+
ignore: str = "",
2627
):
2728
super().__init__([], state)
2829
if ob_args is None:
@@ -35,20 +36,22 @@ def __init__(
3536
self.__texter(text)
3637

3738
@property
39+
@override
3840
def width(self) -> int:
3941
"""
4042
The Texts peak width
4143
"""
4244
return sorted(len(i) for i in self.text.split("\n"))[-1]
4345

4446
@property
47+
@override
4548
def height(self) -> int:
4649
"""
4750
The Texts height
4851
"""
4952
return len(self.text.split("\n"))
5053

51-
def __add__(self, other):
54+
def __add__(self, other: "Text[Any]"):
5255
self.text += other.text
5356
self.obs += other.obs
5457
for obj in self.obs:
@@ -58,7 +61,7 @@ def __add__(self, other):
5861
self.add(self.map, self.x, self.y)
5962
return self
6063

61-
def __texter(self, text):
64+
def __texter(self, text: str):
6265
for txt in text.split("\n"):
6366
for char in txt:
6467
if self.esccode != "":
@@ -69,7 +72,8 @@ def __texter(self, text):
6972
for obj in self.obs:
7073
obj.group = self
7174

72-
def add(self, _map, x, y):
75+
@override
76+
def add(self, _map: Map, x: int, y: int):
7377
"""
7478
Adds the text to a certain coordinate on a certain map.
7579
"""
@@ -84,6 +88,7 @@ def add(self, _map, x, y):
8488
obj.add(self.map, x + i, y + line)
8589
count += len(text)
8690

91+
@override
8792
def remove(self):
8893
"""
8994
Removes the text from the map.
@@ -92,6 +97,7 @@ def remove(self):
9297
for obj in self.obs:
9398
obj.remove()
9499

100+
@override
95101
def rem_ob(self, obj: T):
96102
"""
97103
Removes an object from the group.
@@ -112,7 +118,7 @@ def rem_ob(self, obj: T):
112118
return 0
113119
return 1
114120

115-
def rechar(self, text: str, esccode=""):
121+
def rechar(self, text: str, esccode: str = ""):
116122
"""
117123
Changes the string contained in the text.
118124
"""

0 commit comments

Comments
 (0)