Skip to content

Commit

Permalink
Merge pull request #199 from BurnySc2/add-ruff-and-pyre
Browse files Browse the repository at this point in the history
Add ruff and pyre
  • Loading branch information
BurnySc2 authored Dec 17, 2024
2 parents 343aead + 1056d2b commit 6795c7b
Show file tree
Hide file tree
Showing 106 changed files with 3,665 additions and 3,504 deletions.
19 changes: 11 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,28 +184,28 @@ jobs:
- name: Run autotest_bot.py
# Run bot and list resulting files (replay file, stable_id.json)
run: |
docker run -i -d --name my_container $IMAGE_NAME
docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
docker exec -i my_container bash -c "python test/travis_test_script.py test/autotest_bot.py"
docker exec -i my_container bash -c "tree"
docker rm -f my_container
- name: Run upgradestest_bot.py
run: |
docker run -i -d --name my_container $IMAGE_NAME
docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
docker exec -i my_container bash -c "python test/travis_test_script.py test/upgradestest_bot.py"
docker exec -i my_container bash -c "tree"
docker rm -f my_container
- name: Run damagetest_bot.py
run: |
docker run -i -d --name my_container $IMAGE_NAME
docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
docker exec -i my_container bash -c "python test/travis_test_script.py test/damagetest_bot.py"
docker exec -i my_container bash -c "tree"
docker rm -f my_container
- name: Run queries_test_bot.py
run: |
docker run -i -d --name my_container $IMAGE_NAME
docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
docker exec -i my_container bash -c "python test/travis_test_script.py test/queries_test_bot.py"
docker exec -i my_container bash -c "tree"
docker rm -f my_container
Expand Down Expand Up @@ -234,7 +234,7 @@ jobs:

- name: Run example bots vs computer
run: |
docker run -i -d --name my_container $IMAGE_NAME
docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
docker exec -i my_container bash -c "python test/run_example_bots_vs_computer.py"
docker exec -i my_container bash -c "tree"
docker rm -f my_container
Expand Down Expand Up @@ -266,7 +266,7 @@ jobs:
#
# - name: Run example bots vs each other
# run: |
# docker run -i -d --name my_container $IMAGE_NAME
# docker run -i -d --name my_container --env 'PYTHONPATH=/root/python-sc2' $IMAGE_NAME
# docker exec -i my_container bash -c "python test/run_example_bots_vs_each_other.py"
# docker exec -i my_container bash -c "tree"
# docker rm -f my_container
Expand Down Expand Up @@ -294,11 +294,14 @@ jobs:
run: |
mkdir htmlcov
docker run -i -d \
--mount type=bind,source=$(pwd)/htmlcov,destination=/root/python-sc2/htmlcov \
-v $(pwd)/htmlcov:/root/python-sc2/htmlcov \
--name my_container \
--env 'PYTHONPATH=/root/python-sc2/' \
--entrypoint /bin/bash \
$IMAGE_NAME
echo "Install dev requirements because only non dev requirements exist in the docker image at the moment"
docker exec -i my_container bash -c "uv sync --frozen --no-cache --no-install-project"
docker exec -i my_container bash -c "pip install uv \
&& uv sync --frozen --no-cache --no-install-project"
- name: Run coverage on tests
run: docker exec -i my_container bash -c "uv run pytest --cov=./"
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/docker-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ jobs:
echo "Start container, override the default entrypoint"
docker run -i -d \
--name test_container \
--env 'PYTHONPATH=/root/python-sc2/' \
--entrypoint /bin/bash \
$IMAGE_NAME-v$VERSION_NUMBER-squashed
echo "Install python-sc2"
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ mini_games/
/htmlcov

/docs

.pyre
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,27 @@ repos:
- id: python-no-log-warn
# Enforce type annotation instead of comment annotation
- id: python-use-type-annotations

- repo: local
hooks:
# Autoformat code
- id: ruff-format-check
name: Check if files are formatted
stages: [push]
language: system
entry: uv run ruff format . --check --diff
pass_filenames: false

- id: ruff-lint
name: Lint files
stages: [push]
language: system
entry: uv run ruff check .
pass_filenames: false

- id: pyre
name: Static types checking with pyre
stages: [push]
language: system
entry: uv run pyre
pass_filenames: false
17 changes: 17 additions & 0 deletions .pyre_configuration
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"site_package_search_strategy": "pep561",
"source_directories": [
{
"import_root": ".",
"source": "sc2"
},
{
"import_root": ".",
"source": "examples"
},
{
"import_root": ".",
"source": "test"
}
]
}
7 changes: 4 additions & 3 deletions dockerfiles/test_docker_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@ docker rm -f test_container
# Start container, override the default entrypoint
docker run -i -d \
--name test_container \
--env 'PYTHONPATH=/root/python-sc2' \
--entrypoint /bin/bash \
$IMAGE_NAME

# Install requirements
docker exec -i test_container mkdir -p /root/python-sc2
docker cp pyproject.toml test_container:/root/python-sc2/
docker cp uv.lock test_container:/root/python-sc2/
docker exec -i test_container bash -c "pip install uv && cd python-sc2 && uv sync --no-cache --no-install-project"

docker cp sc2 test_container:/root/python-sc2/sc2
docker cp test test_container:/root/python-sc2/test

# Install python-sc2, via mount the python-sc2 folder will be available
docker exec -i test_container bash -c "pip install uv && cd python-sc2 && uv sync --no-cache --no-install-project"

# Run various test bots
docker exec -i test_container bash -c "cd python-sc2 && uv run python test/travis_test_script.py test/autotest_bot.py"
docker exec -i test_container bash -c "cd python-sc2 && uv run python test/travis_test_script.py test/queries_test_bot.py"
Expand Down
6 changes: 4 additions & 2 deletions docs_generate/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import os
import sys

sys.path.insert(0, os.path.abspath(".."))
sys.path.insert(0, os.path.abspath("..")) # noqa: PTH100

import sphinx_rtd_theme # nopycln: import
import sphinx_rtd_theme

sphinx_rtd_theme # Add statement to keep unused import

# -- Project information -----------------------------------------------------

Expand Down
6 changes: 3 additions & 3 deletions docs_generate/text_files/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ Information about the enemy player::
self.enemy_structures: Units

# Enemy spawn locations as a list of Point2 points
self.enemy_start_locations: List[Point2]
self.enemy_start_locations: list[Point2]

# Enemy units that are inside your sensor tower range
self.blips: Set[Blip]
self.blips: set[Blip]

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

# Locations of possible expansions
self.expansion_locations: Dict[Point2, Units]
self.expansion_locations: dict[Point2, Units]

# Game data about units, abilities and upgrades (see game_data.py)
self.game_data: GameData
Expand Down
16 changes: 9 additions & 7 deletions examples/arcade_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
- Make marines constantly run if they have a ling/bane very close to them
- Split marines before engaging
"""
from typing import Union

from __future__ import annotations

from loguru import logger

Expand All @@ -37,17 +38,14 @@


class MarineSplitChallenge(BotAI):

async def on_start(self):
await self.chat_send("Edit this message for automatic chat commands.")
self.client.game_step = 2

async def on_step(self, iteration):
# do marine micro vs zerglings
for unit in self.units(UnitTypeId.MARINE):

if self.enemy_units:

# attack (or move towards) zerglings / banelings
if unit.weapon_cooldown <= self.client.game_step / 2:
enemies_in_range = self.enemy_units.filter(unit.target_in_range)
Expand All @@ -57,7 +55,8 @@ async def on_step(self, iteration):
# Use stimpack
if (
self.already_pending_upgrade(UpgradeId.STIMPACK) == 1
and not unit.has_buff(BuffId.STIMPACK) and unit.health > 10
and not unit.has_buff(BuffId.STIMPACK)
and unit.health > 10
):
unit(AbilityId.EFFECT_STIM)

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

def position_around_unit(
self,
pos: Union[Unit, Point2, Point3],
pos: Unit | Point2 | Point3,
distance: int = 1,
step_size: int = 1,
exclude_out_of_bounds: bool = True,
):
# pyre-ignore[16]
pos = pos.position.rounded
positions = {
pos.offset(Point2((x, y)))
for x in range(-distance, distance + 1, step_size) for y in range(-distance, distance + 1, step_size)
for x in range(-distance, distance + 1, step_size)
for y in range(-distance, distance + 1, step_size)
if (x, y) != (0, 0)
}
# filter positions outside map size
if exclude_out_of_bounds:
positions = {
p
for p in positions
# pyre-ignore[16]
if 0 <= p[0] < self.game_info.pathing_grid.width and 0 <= p[1] < self.game_info.pathing_grid.height
}
return positions
Expand Down
5 changes: 3 additions & 2 deletions examples/bot_vs_bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""
This script shows how to let two custom bots play against each other.
"""
from typing import List

from __future__ import annotations

from loguru import logger

Expand All @@ -14,7 +15,7 @@


def main_old():
result: List[Result] = run_game(
result: list[Result] = run_game(
maps.get("AcropolisLE"),
[
Bot(Race.Protoss, WarpGateBot()),
Expand Down
10 changes: 3 additions & 7 deletions examples/competitive/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# pylint: disable=W0212
import argparse
import asyncio

Expand All @@ -7,7 +6,7 @@

import sc2
from sc2.client import Client
from sc2.protocol import ConnectionAlreadyClosed
from sc2.protocol import ConnectionAlreadyClosedError


# Run ladder game
Expand All @@ -26,10 +25,7 @@ def run_ladder_game(bot):
parser.add_argument("--RealTime", action="store_true", help="Real time flag")
args, _unknown = parser.parse_known_args()

if args.LadderServer is None:
host = "127.0.0.1"
else:
host = args.LadderServer
host = "127.0.0.1" if args.LadderServer is None else args.LadderServer

host_port = args.GamePort
lan_port = args.StartPort
Expand Down Expand Up @@ -68,7 +64,7 @@ async def join_ladder_game(host, port, players, realtime, portconfig, save_repla
await client.save_replay(save_replay_as)
# await client.leave()
# await client.quit()
except ConnectionAlreadyClosed:
except ConnectionAlreadyClosedError:
logger.error("Connection was closed before the game ended")
return None
finally:
Expand Down
2 changes: 1 addition & 1 deletion examples/competitive/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


class CompetitiveBot(BotAI):

async def on_start(self):
print("Game started")
# Do things here before the game starts
Expand All @@ -12,6 +11,7 @@ async def on_step(self, iteration):
# Populate this function with whatever your bot should do!
pass

# pyre-ignore[11]
async def on_end(self, game_result: Result):
print("Game ended.")
# Do things here after the game ends
2 changes: 1 addition & 1 deletion examples/competitive/run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=E0401
# pyre-ignore-all-errors[16, 21]
import sys

from __init__ import run_ladder_game
Expand Down
2 changes: 1 addition & 1 deletion examples/distributed_workers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pyre-ignore-all-errors[16]
from sc2 import maps
from sc2.bot_ai import BotAI
from sc2.data import Difficulty, Race
Expand All @@ -7,7 +8,6 @@


class TerranBot(BotAI):

async def on_step(self, iteration):
await self.distribute_workers()
await self.build_supply()
Expand Down
3 changes: 1 addition & 2 deletions examples/observer_easy_vs_easy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
def main():
run_game(
maps.get("Abyssal Reef LE"),
[Bot(Race.Protoss, CannonRushBot()),
Computer(Race.Protoss, Difficulty.Medium)],
[Bot(Race.Protoss, CannonRushBot()), Computer(Race.Protoss, Difficulty.Medium)],
realtime=True,
)

Expand Down
5 changes: 1 addition & 4 deletions examples/protoss/cannon_rush.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@


class CannonRushBot(BotAI):

# pylint: disable=R0912
async def on_step(self, iteration):
if iteration == 0:
await self.chat_send("(probe)(pylon)(cannon)(cannon)(gg)")
Expand Down Expand Up @@ -64,8 +62,7 @@ async def on_step(self, iteration):
def main():
run_game(
maps.get("(2)CatalystLE"),
[Bot(Race.Protoss, CannonRushBot(), name="CheeseCannon"),
Computer(Race.Protoss, Difficulty.Medium)],
[Bot(Race.Protoss, CannonRushBot(), name="CheeseCannon"), Computer(Race.Protoss, Difficulty.Medium)],
realtime=False,
)

Expand Down
Loading

0 comments on commit 6795c7b

Please sign in to comment.