Skip to content

Commit

Permalink
Making vscode python typechecker happy on strict mode (for src/.../*.…
Browse files Browse the repository at this point in the history
…py files only) (#399)

* initial commit

* bring flake8 file back

* fix project name

* remove newline

* finished copying core elements of setup.py

* organization

* not including py.typed yet

* taking all changes in src/ from commit 3d88246

* apply isort to entire repo

* add isort into infrastructure

* fixing line length limit

* fixing line length limit

* isort

* made progress fixing types further

* isort

* removing pyre-blocking comments

* isort

* reverting player_network_interface.py changes

* apply isort to all python files outside of src

* fix CI test

* bringing types back to being compatible with python 3.7

* black

* tab is 4 spaces

* try replacing pyre with pyright

* removing lint ignore comments

* fixing circular import

* add necessary import

* fixed unit test

* fixing type expression

* black, isort

* fix unit test

* fix unit test

* attempt to fix unittest run

* fix counter

* testing

* more testing

* fixed unit test

* updating unit tests

* fix recursion error

* fixing recursion

* unit test fix

* fix unit test

* removing ` -> None`

* black

* removing accidental second method declaration

* UNKNOWN is never protected

* fix unit test

* fix unit test

* revert bad change

* fix unit test

* fixed inconsistent naming

* fixed naming error

* fix broken status setter

* refactor

* this part of the unit test doesn't make sense now

* removing unit tests that check types

* fixed unit test

* fixed method

* black, isort

* updated calls to fixed method

* adding types

* trying to fix unit test issue

* trying to fix unit test

* fixing unit test

* isort, fixed __deepcopy__

* black, isort

* adding in py.typed file

* organize

* remove numpy dependency

* remove unnecessary unit test segment

* fixing types

* formatting and improving type annotations

* getting rid of Any's when possible

* fixing unit test

* fix type annotations

* bring back action space

* bringing back NamedTuple usage

* fixing types

* fixing init.py

* fix unit test

* fixing init.py

* fix unit test

* git rid of pyre comments

* fixing the rest of pyright's complaints

* bring pyright to most recent version

* get rid of unused import

* removing unnecessary None return annotations

* black

* changing all can_terastallize into can_tera

* moved docstring to correct location

* removing unnecessary PlayerType typevar

* updating pyright
  • Loading branch information
cameronangliss committed Sep 23, 2023
1 parent 258d36d commit c0fd4ac
Show file tree
Hide file tree
Showing 53 changed files with 995 additions and 1,060 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ jobs:
- name: Run isort
run: isort --check .

- name: Run pyre
run: pyre check
- name: Run pyright
run: pyright src/

- name: Build docs
run: |
Expand Down
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ venv.bak/
*.code-workspace
.vscode*

.pyre/*

# Mac os
*.DS_Store

Expand Down
4 changes: 0 additions & 4 deletions .pyre_configuration

This file was deleted.

1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ include src/poke_env/data/static/*.html
include src/poke_env/data/static/moves/*.json
include src/poke_env/data/static/pokedex/*.json
include src/poke_env/data/static/typechart/*.json
include src/poke_env/py.typed
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ line-length = 88
profile = "black"
multi_line_output = 3

[tool.pyright]
include = ["src"]
pythonVersion = "3.7"

[tool.setuptools.dynamic]
dependencies = { file = ["requirements.txt"] }
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ black==23.9.1
flake8
isort==5.12.0
pre-commit
pyre-check==0.9.18
pyright==1.1.324
pytest
pytest-asyncio
pytest-cov
Expand Down
7 changes: 1 addition & 6 deletions src/poke_env/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import poke_env.environment as environment
import poke_env.exceptions as exceptions
import poke_env.player as player
import poke_env.ps_client as ps_client
import poke_env.stats as stats
import poke_env.teambuilder as teambuilder
from poke_env.data import gen_data, to_id_str
Expand All @@ -27,21 +28,15 @@
__all__ = [
"AccountConfiguration",
"LocalhostServerConfiguration",
"MOVES",
"NATURES",
"POKEDEX",
"ServerConfiguration",
"ShowdownException",
"ShowdownServerConfiguration",
"UNKNOWN_ITEM",
"compute_raw_stats",
"environment",
"exceptions",
"gen_data",
"player",
"player_configuration",
"ps_client",
"server_configuration",
"stats",
"teambuilder",
"to_id_str",
Expand Down
25 changes: 11 additions & 14 deletions src/poke_env/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
import sys
from logging import CRITICAL, disable
from threading import Thread
from typing import Any, List


def __run_loop(loop: asyncio.AbstractEventLoop):
asyncio.set_event_loop(loop)
loop.run_forever()


def __stop_loop(loop: asyncio.AbstractEventLoop, thread: Thread): # pragma: no cover
def __stop_loop(loop: asyncio.AbstractEventLoop, thread: Thread):
disable(CRITICAL)
tasks = []
if py_ver.major == 3 and py_ver.minor >= 7:
caller = asyncio
else:
caller = asyncio.Task
for task in caller.all_tasks(loop):
tasks: List[asyncio.Task[Any]] = []
for task in asyncio.all_tasks(loop):
task.cancel()
tasks.append(task)

Expand All @@ -36,15 +33,15 @@ def __stop_loop(loop: asyncio.AbstractEventLoop, thread: Thread): # pragma: no
loop.call_soon_threadsafe(loop.close)


def __clear_loop(): # pragma: no cover
def __clear_loop():
__stop_loop(POKE_LOOP, _t)


async def _create_in_poke_loop_async(cls, *args, **kwargs):
return cls(*args, **kwargs)
async def _create_in_poke_loop_async(cls_: Any, *args: Any, **kwargs: Any) -> Any:
return cls_(*args, **kwargs)


def create_in_poke_loop(cls, *args, **kwargs): # pragma: no cover
def create_in_poke_loop(cls_: Any, *args: Any, **kwargs: Any) -> Any:
try:
# Python >= 3.7
loop = asyncio.get_running_loop()
Expand All @@ -55,14 +52,14 @@ def create_in_poke_loop(cls, *args, **kwargs): # pragma: no cover
# asyncio.get_running_loop raised exception so no loop is running
loop = None
if loop == POKE_LOOP:
return cls(*args, **kwargs)
return cls_(*args, **kwargs)
else:
return asyncio.run_coroutine_threadsafe(
_create_in_poke_loop_async(cls, *args, **kwargs), POKE_LOOP
_create_in_poke_loop_async(cls_, *args, **kwargs), POKE_LOOP
).result()


async def handle_threaded_coroutines(coro):
async def handle_threaded_coroutines(coro: Any):
task = asyncio.run_coroutine_threadsafe(coro, POKE_LOOP)
await asyncio.wrap_future(task)
return task.result()
Expand Down
4 changes: 2 additions & 2 deletions src/poke_env/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from poke_env.data.gen_data import GenData
from poke_env.data.normalize import to_id_str
from poke_env.data.replay_template import _REPLAY_TEMPLATE
from poke_env.data.replay_template import REPLAY_TEMPLATE

__all__ = [
"_REPLAY_TEMPLATE",
"REPLAY_TEMPLATE",
"GenData",
"to_id_str",
]
18 changes: 10 additions & 8 deletions src/poke_env/data/gen_data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import os
from functools import lru_cache
from typing import Any, Dict, Union
from typing import Any, Dict, Optional, Union

import orjson # pyre-ignore[21]
import orjson

from poke_env.data.normalize import to_id_str

Expand All @@ -12,9 +14,9 @@ class GenData:

UNKNOWN_ITEM = "unknown_item"

_gen_data_per_gen = {}
_gen_data_per_gen: Dict[int, GenData] = {}

def __init__(self, gen: int) -> None:
def __init__(self, gen: int):
if gen in self._gen_data_per_gen:
raise ValueError(f"GenData for gen {gen} already initialized.")

Expand All @@ -25,7 +27,7 @@ def __init__(self, gen: int) -> None:
self.type_chart = self.load_type_chart(gen)
self.learnset = self.load_learnset()

def __deepcopy__(self, memodict=None) -> "GenData":
def __deepcopy__(self, memodict: Optional[Dict[int, Any]] = None) -> GenData:
return self

def load_moves(self, gen: int) -> Dict[str, Any]:
Expand All @@ -49,7 +51,7 @@ def load_pokedex(self, gen: int) -> Dict[str, Any]:
dex = orjson.loads(f.read())

other_forms_dex: Dict[str, Any] = {}
for key, value in dex.items():
for value in dex.values():
if "cosmeticFormes" in value:
for other_form in value["cosmeticFormes"]:
other_forms_dex[to_id_str(other_form)] = value
Expand Down Expand Up @@ -113,14 +115,14 @@ def _static_files_root(self) -> str:

@classmethod
@lru_cache(None)
def from_gen(cls, gen: int) -> "GenData":
def from_gen(cls, gen: int) -> GenData:
gen_data = GenData(gen)
cls._gen_data_per_gen[gen] = gen_data

return gen_data

@classmethod
@lru_cache(None)
def from_format(cls, format: str) -> "GenData":
def from_format(cls, format: str) -> GenData:
gen = int(format[3]) # Update when Gen 10 comes
return cls.from_gen(gen)
2 changes: 1 addition & 1 deletion src/poke_env/data/replay_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
os.path.dirname(os.path.realpath(__file__)), "static", "replay_template.html"
)
) as f:
_REPLAY_TEMPLATE = f.read()
REPLAY_TEMPLATE = f.read()
18 changes: 0 additions & 18 deletions src/poke_env/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,6 @@
"Effect",
"EmptyMove",
"Field",
"GEN_TO_MOVES",
"GEN_TO_MOVE_CLASS",
"GEN_TO_POKEMON",
"Gen4Battle",
"Gen4Move",
"Gen4Pokemon",
"Gen5Battle",
"Gen5Move",
"Gen5Pokemon",
"Gen6Battle",
"Gen6Move",
"Gen6Pokemon",
"Gen7Battle",
"Gen7Move",
"Gen7Pokemon",
"Gen8Battle",
"Gen8Move",
"Gen8Pokemon",
"Move",
"MoveCategory",
"Pokemon",
Expand Down

0 comments on commit c0fd4ac

Please sign in to comment.