Skip to content

Commit 6795c7b

Browse files
authored
Merge pull request #199 from BurnySc2/add-ruff-and-pyre
Add ruff and pyre
2 parents 343aead + 1056d2b commit 6795c7b

File tree

106 files changed

+3665
-3504
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+3665
-3504
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,28 +184,28 @@ jobs:
184184
- name: Run autotest_bot.py
185185
# Run bot and list resulting files (replay file, stable_id.json)
186186
run: |
187-
docker run -i -d --name my_container $IMAGE_NAME
187+
docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
188188
docker exec -i my_container bash -c "python test/travis_test_script.py test/autotest_bot.py"
189189
docker exec -i my_container bash -c "tree"
190190
docker rm -f my_container
191191
192192
- name: Run upgradestest_bot.py
193193
run: |
194-
docker run -i -d --name my_container $IMAGE_NAME
194+
docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
195195
docker exec -i my_container bash -c "python test/travis_test_script.py test/upgradestest_bot.py"
196196
docker exec -i my_container bash -c "tree"
197197
docker rm -f my_container
198198
199199
- name: Run damagetest_bot.py
200200
run: |
201-
docker run -i -d --name my_container $IMAGE_NAME
201+
docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
202202
docker exec -i my_container bash -c "python test/travis_test_script.py test/damagetest_bot.py"
203203
docker exec -i my_container bash -c "tree"
204204
docker rm -f my_container
205205
206206
- name: Run queries_test_bot.py
207207
run: |
208-
docker run -i -d --name my_container $IMAGE_NAME
208+
docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
209209
docker exec -i my_container bash -c "python test/travis_test_script.py test/queries_test_bot.py"
210210
docker exec -i my_container bash -c "tree"
211211
docker rm -f my_container
@@ -234,7 +234,7 @@ jobs:
234234

235235
- name: Run example bots vs computer
236236
run: |
237-
docker run -i -d --name my_container $IMAGE_NAME
237+
docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
238238
docker exec -i my_container bash -c "python test/run_example_bots_vs_computer.py"
239239
docker exec -i my_container bash -c "tree"
240240
docker rm -f my_container
@@ -266,7 +266,7 @@ jobs:
266266
#
267267
# - name: Run example bots vs each other
268268
# run: |
269-
# docker run -i -d --name my_container $IMAGE_NAME
269+
# docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
270270
# docker exec -i my_container bash -c "python test/run_example_bots_vs_each_other.py"
271271
# docker exec -i my_container bash -c "tree"
272272
# docker rm -f my_container
@@ -294,11 +294,14 @@ jobs:
294294
run: |
295295
mkdir htmlcov
296296
docker run -i -d \
297-
--mount type=bind,source=$(pwd)/htmlcov,destination=/root/python-sc2/htmlcov \
297+
-v $(pwd)/htmlcov:/root/python-sc2/htmlcov \
298298
--name my_container \
299+
--env 'PYTHONPATH=/root/python-sc2/' \
300+
--entrypoint /bin/bash \
299301
$IMAGE_NAME
300302
echo "Install dev requirements because only non dev requirements exist in the docker image at the moment"
301-
docker exec -i my_container bash -c "uv sync --frozen --no-cache --no-install-project"
303+
docker exec -i my_container bash -c "pip install uv \
304+
&& uv sync --frozen --no-cache --no-install-project"
302305
303306
- name: Run coverage on tests
304307
run: docker exec -i my_container bash -c "uv run pytest --cov=./"

.github/workflows/docker-ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ jobs:
103103
echo "Start container, override the default entrypoint"
104104
docker run -i -d \
105105
--name test_container \
106+
--env 'PYTHONPATH=/root/python-sc2/' \
106107
--entrypoint /bin/bash \
107108
$IMAGE_NAME-v$VERSION_NUMBER-squashed
108109
echo "Install python-sc2"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ mini_games/
2929
/htmlcov
3030

3131
/docs
32+
33+
.pyre

.pre-commit-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,27 @@ repos:
3232
- id: python-no-log-warn
3333
# Enforce type annotation instead of comment annotation
3434
- id: python-use-type-annotations
35+
36+
- repo: local
37+
hooks:
38+
# Autoformat code
39+
- id: ruff-format-check
40+
name: Check if files are formatted
41+
stages: [push]
42+
language: system
43+
entry: uv run ruff format . --check --diff
44+
pass_filenames: false
45+
46+
- id: ruff-lint
47+
name: Lint files
48+
stages: [push]
49+
language: system
50+
entry: uv run ruff check .
51+
pass_filenames: false
52+
53+
- id: pyre
54+
name: Static types checking with pyre
55+
stages: [push]
56+
language: system
57+
entry: uv run pyre
58+
pass_filenames: false

.pyre_configuration

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"site_package_search_strategy": "pep561",
3+
"source_directories": [
4+
{
5+
"import_root": ".",
6+
"source": "sc2"
7+
},
8+
{
9+
"import_root": ".",
10+
"source": "examples"
11+
},
12+
{
13+
"import_root": ".",
14+
"source": "test"
15+
}
16+
]
17+
}

dockerfiles/test_docker_image.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,19 @@ docker rm -f test_container
3535
# Start container, override the default entrypoint
3636
docker run -i -d \
3737
--name test_container \
38+
--env 'PYTHONPATH=/root/python-sc2' \
3839
--entrypoint /bin/bash \
3940
$IMAGE_NAME
4041

42+
# Install requirements
4143
docker exec -i test_container mkdir -p /root/python-sc2
4244
docker cp pyproject.toml test_container:/root/python-sc2/
4345
docker cp uv.lock test_container:/root/python-sc2/
46+
docker exec -i test_container bash -c "pip install uv && cd python-sc2 && uv sync --no-cache --no-install-project"
47+
4448
docker cp sc2 test_container:/root/python-sc2/sc2
4549
docker cp test test_container:/root/python-sc2/test
4650

47-
# Install python-sc2, via mount the python-sc2 folder will be available
48-
docker exec -i test_container bash -c "pip install uv && cd python-sc2 && uv sync --no-cache --no-install-project"
49-
5051
# Run various test bots
5152
docker exec -i test_container bash -c "cd python-sc2 && uv run python test/travis_test_script.py test/autotest_bot.py"
5253
docker exec -i test_container bash -c "cd python-sc2 && uv run python test/travis_test_script.py test/queries_test_bot.py"

docs_generate/conf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import os
1414
import sys
1515

16-
sys.path.insert(0, os.path.abspath(".."))
16+
sys.path.insert(0, os.path.abspath("..")) # noqa: PTH100
1717

18-
import sphinx_rtd_theme # nopycln: import
18+
import sphinx_rtd_theme
19+
20+
sphinx_rtd_theme # Add statement to keep unused import
1921

2022
# -- Project information -----------------------------------------------------
2123

docs_generate/text_files/introduction.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ Information about the enemy player::
7979
self.enemy_structures: Units
8080

8181
# Enemy spawn locations as a list of Point2 points
82-
self.enemy_start_locations: List[Point2]
82+
self.enemy_start_locations: list[Point2]
8383

8484
# Enemy units that are inside your sensor tower range
85-
self.blips: Set[Blip]
85+
self.blips: set[Blip]
8686

8787
# The enemy race. If the enemy chose random, this will stay at random forever
8888
self.enemy_race: Race
@@ -98,7 +98,7 @@ Other information::
9898
self.all_units: Units # All units combined: yours, enemy's and neutral
9999

100100
# Locations of possible expansions
101-
self.expansion_locations: Dict[Point2, Units]
101+
self.expansion_locations: dict[Point2, Units]
102102

103103
# Game data about units, abilities and upgrades (see game_data.py)
104104
self.game_data: GameData

examples/arcade_bot.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
- Make marines constantly run if they have a ling/bane very close to them
2020
- Split marines before engaging
2121
"""
22-
from typing import Union
22+
23+
from __future__ import annotations
2324

2425
from loguru import logger
2526

@@ -37,17 +38,14 @@
3738

3839

3940
class MarineSplitChallenge(BotAI):
40-
4141
async def on_start(self):
4242
await self.chat_send("Edit this message for automatic chat commands.")
4343
self.client.game_step = 2
4444

4545
async def on_step(self, iteration):
4646
# do marine micro vs zerglings
4747
for unit in self.units(UnitTypeId.MARINE):
48-
4948
if self.enemy_units:
50-
5149
# attack (or move towards) zerglings / banelings
5250
if unit.weapon_cooldown <= self.client.game_step / 2:
5351
enemies_in_range = self.enemy_units.filter(unit.target_in_range)
@@ -57,7 +55,8 @@ async def on_step(self, iteration):
5755
# Use stimpack
5856
if (
5957
self.already_pending_upgrade(UpgradeId.STIMPACK) == 1
60-
and not unit.has_buff(BuffId.STIMPACK) and unit.health > 10
58+
and not unit.has_buff(BuffId.STIMPACK)
59+
and unit.health > 10
6160
):
6261
unit(AbilityId.EFFECT_STIM)
6362

@@ -97,22 +96,25 @@ async def on_step(self, iteration):
9796

9897
def position_around_unit(
9998
self,
100-
pos: Union[Unit, Point2, Point3],
99+
pos: Unit | Point2 | Point3,
101100
distance: int = 1,
102101
step_size: int = 1,
103102
exclude_out_of_bounds: bool = True,
104103
):
104+
# pyre-ignore[16]
105105
pos = pos.position.rounded
106106
positions = {
107107
pos.offset(Point2((x, y)))
108-
for x in range(-distance, distance + 1, step_size) for y in range(-distance, distance + 1, step_size)
108+
for x in range(-distance, distance + 1, step_size)
109+
for y in range(-distance, distance + 1, step_size)
109110
if (x, y) != (0, 0)
110111
}
111112
# filter positions outside map size
112113
if exclude_out_of_bounds:
113114
positions = {
114115
p
115116
for p in positions
117+
# pyre-ignore[16]
116118
if 0 <= p[0] < self.game_info.pathing_grid.width and 0 <= p[1] < self.game_info.pathing_grid.height
117119
}
118120
return positions

examples/bot_vs_bot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""
22
This script shows how to let two custom bots play against each other.
33
"""
4-
from typing import List
4+
5+
from __future__ import annotations
56

67
from loguru import logger
78

@@ -14,7 +15,7 @@
1415

1516

1617
def main_old():
17-
result: List[Result] = run_game(
18+
result: list[Result] = run_game(
1819
maps.get("AcropolisLE"),
1920
[
2021
Bot(Race.Protoss, WarpGateBot()),

0 commit comments

Comments
 (0)