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

[WIP] Make mypy happy #181

Open
wants to merge 22 commits into
base: canon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ before_install:

install:
- pip install -e .;
- pip install pytest;
- pip install pytest mypy;

script:
- python -c "print(':'.join(p for p in __import__('sys').path))"
- export MYPYPATH=/home/travis/virtualenv/python3.6.3/lib/python3.6:/home/travis/build/ppb/pursuedpybear
- pytest
- mypy ppb tests
82 changes: 0 additions & 82 deletions ppb/abc.py

This file was deleted.

11 changes: 5 additions & 6 deletions ppb/camera.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Sequence
from typing import Union
from numbers import Number

from ppb import Vector
from ppb.sprites import BaseSprite
Expand Down Expand Up @@ -34,19 +33,19 @@ def __init__(self, viewport: Sequence[int]=(0, 0, 800, 600),
self.pixel_ratio = pixel_ratio

@property
def frame_top(self) -> Number:
def frame_top(self) -> Union[int, float]:
return self.position.y - self.half_height

@property
def frame_bottom(self) -> Number:
def frame_bottom(self) -> Union[int, float]:
return self.position.y + self.half_height

@property
def frame_left(self) -> Number:
def frame_left(self) -> Union[int, float]:
return self.position.x - self.half_width

@property
def frame_right(self) -> Number:
def frame_right(self) -> Union[int, float]:
return self.position.x + self.half_width

@property
Expand Down Expand Up @@ -82,7 +81,7 @@ def viewport_height(self, value: int):
self._viewport_height = value
self.viewport_offset = Vector(self.viewport_width / 2, value / 2)

def point_in_viewport(self, point:Vector) -> bool:
def point_in_viewport(self, point: Vector) -> bool:
px, py = point
vpx, vpy = self.viewport_origin
vpw = self.viewport_width
Expand Down
9 changes: 7 additions & 2 deletions ppb/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import time
from typing import Callable
from typing import Type
from typing import Mapping
from typing import Any

from ppb.abc import Engine
from ppb.events import EventMixin
from ppb.events import Quit
from ppb.systems import PygameEventPoller
Expand All @@ -15,7 +16,11 @@
from ppb.utils import LoggingMixin


class GameEngine(Engine, EventMixin, LoggingMixin):
class GameEngine(EventMixin, LoggingMixin):
scenes: list
events: deque
event_extensions: Mapping[Type, Mapping[str, Any]]
systems: list

def __init__(self, first_scene: Type, *,
systems=(Renderer, Updater, PygameEventPoller),
Expand Down
30 changes: 15 additions & 15 deletions ppb/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import re
from typing import Collection
from typing import Set

from ppb.abc import Scene
from ppb.buttons import MouseButton
from ppb.keycodes import KeyCode
from ppb.vector import Vector
from typing import Optional

__all__ = (
'EventMixin',
Expand Down Expand Up @@ -73,9 +69,13 @@ def __event__(self, bag, fire_event):
else:
raise

# Import these late so we don't have circular import problems.
from ppb.buttons import MouseButton
from ppb.keycodes import KeyCode
from ppb.vector import Vector
from ppb.scenes import BaseScene

# Remember to define scene at the end so the pargs version of __init__() still works

@dataclass
class ButtonPressed:
"""
Expand All @@ -84,7 +84,7 @@ class ButtonPressed:
button: MouseButton
position: Vector # Scene position
# TODO: Add frame position
scene: Scene = None
scene: Optional[BaseScene] = None


@dataclass
Expand All @@ -95,21 +95,21 @@ class ButtonReleased:
button: MouseButton
position: Vector # Scene position
# TODO: Add frame position
scene: Scene = None
scene: Optional[BaseScene] = None


@dataclass
class KeyPressed:
key: KeyCode
mods: Set[KeyCode]
scene: Scene = None
scene: Optional[BaseScene] = None


@dataclass
class KeyReleased:
key: KeyCode
mods: Set[KeyCode]
scene: Scene = None
scene: Optional[BaseScene] = None

@dataclass
class MouseMotion:
Expand All @@ -118,31 +118,31 @@ class MouseMotion:
screen_position: Vector
delta: Vector
buttons: Collection[MouseButton]
scene: Scene = None
scene: Optional[BaseScene] = None


@dataclass
class PreRender:
"""
Fired before rendering.
"""
scene: Scene = None
scene: Optional[BaseScene] = None


@dataclass
class Quit:
"""
Fired on an OS Quit event.
"""
scene: Scene = None
scene: Optional[BaseScene] = None


@dataclass
class Render:
"""
Fired at render.
"""
scene: Scene = None
scene: Optional[BaseScene] = None


@dataclass
Expand All @@ -151,4 +151,4 @@ class Update:
Fired on game tick
"""
time_delta: float
scene: Scene = None
scene: Optional[BaseScene] = None
14 changes: 9 additions & 5 deletions ppb/scenes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections import defaultdict
from collections.abc import Collection
from numbers import Number
from typing import Callable
from typing import Hashable
from typing import Iterable
Expand All @@ -9,8 +8,9 @@
from typing import Tuple
from typing import Type
from typing import Union
from typing import Optional

from ppb.abc import Scene
import ppb.engine
from ppb.camera import Camera
from ppb.events import EventMixin

Expand Down Expand Up @@ -96,15 +96,19 @@ def remove(self, game_object: Hashable) -> None:
s.discard(game_object)


class BaseScene(Scene, EventMixin):
class BaseScene(EventMixin):
# Background color, in RGB, each channel is 0-255
background_color: Sequence[int] = (0, 0, 100)
container_class: Type = GameObjectCollection
engine: 'ppb.engine.GameEngine'
running: bool = True
next: Optional[Type] = None

def __init__(self, engine, *,
set_up: Callable=None, pixel_ratio: Number=80,
set_up: Callable=None, pixel_ratio: Union[int, float]=80,
**kwargs):
super().__init__(engine)
self.engine = engine

for k, v in kwargs.items():
setattr(self, k, v)

Expand Down
20 changes: 13 additions & 7 deletions ppb/sprites.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from inspect import getfile
from numbers import Number
from os.path import realpath
from pathlib import Path
from typing import Dict, Iterable, AnyStr, Sequence
from typing import Dict, Iterable, Sequence
from typing import Union
from typing import Optional
from typing import Type
from typing import Any

from ppb import Vector
from ppb.events import EventMixin
from ppb.flags import DoNotRender


TOP = "top"
Expand All @@ -26,7 +29,10 @@ class Side:
BOTTOM: ('y', 1)
}

def __init__(self, parent: 'BaseSprite',side: AnyStr):
side: str
parent: 'BaseSprite'

def __init__(self, parent: 'BaseSprite', side: str):
self.side = side
self.parent = parent

Expand Down Expand Up @@ -142,14 +148,14 @@ def _attribute_gate(self, attribute, bad_sides):

class BaseSprite(EventMixin):

image = None
resource_path = None
image: Union[str, Any, None] = None # Any is actually DoNotRender
resource_path: Union[str, Path, None] = None

def __init__(self, size: Union[int, float]=1, pos: Iterable=(0, 0),
facing: Vector=Vector(0, -1)):
super().__init__()
self.position = Vector(*pos)
self._offset_value = None
self._offset_value = 0
self._size = None
self.size = size
self.facing = facing
Expand Down Expand Up @@ -206,7 +212,7 @@ def size(self, value):
self._size = value
self._offset_value = self._size / 2

def rotate(self, degrees: Number):
def rotate(self, degrees: float):
self.facing.rotate(degrees)

def __image__(self):
Expand Down
4 changes: 4 additions & 0 deletions ppb/systems/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random
import time
import typing

import pygame

Expand Down Expand Up @@ -28,6 +29,9 @@ def activate(self, engine):


class Renderer(System):
resources: typing.Mapping[str, typing.Any]
resized_images: typing.Mapping[str, typing.Any]
old_resized_images: typing.Mapping[str, typing.Any]

def __init__(self, resolution=default_resolution, window_title: str="PursuedPyBear", **kwargs):
self.resolution = resolution
Expand Down
Loading