Skip to content

Commit

Permalink
add pre-commit-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerYep committed Jul 10, 2021
1 parent ac733a1 commit 2bb2dbe
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 48 deletions.
36 changes: 18 additions & 18 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
ci:
skip: [mypy, pylint, pytest]
repos:
- repo: local
- repo: https://github.com/asottile/pyupgrade
rev: v2.20.0
hooks:
- id: pyupgrade
name: pyupgrade
entry: pyupgrade --py38-plus
language: python
types: [python]
- id: pyupgrade
args: [--py38-plus]

- repo: https://github.com/PyCQA/isort
rev: 5.9.1
hooks:
- id: isort
name: isort
entry: isort
language: python
types: [python]

- repo: https://github.com/psf/black
rev: 21.6b0
hooks:
- id: black
name: black
entry: black -C
language: python
types: [python]
args: [-C]

- repo: https://github.com/PyCQA/flake8
rev: 3.9.2
hooks:
- id: flake8
name: flake8
entry: flake8
language: python
types: [python]

- repo: local
hooks:
- id: mypy
name: mypy
entry: mypy
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# stanford-karel

[![Python 3.7+](https://img.shields.io/badge/python-3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/)
[![Build Status](https://github.com/TylerYep/stanfordkarel/actions/workflows/test.yml/badge.svg)](https://github.com/TylerYep/stanfordkarel/actions/workflows/test.yml)
[![PyPI version](https://badge.fury.io/py/stanfordkarel.svg)](https://badge.fury.io/py/stanfordkarel)
[![Build Status](https://github.com/TylerYep/stanfordkarel/actions/workflows/test.yml/badge.svg)](https://github.com/TylerYep/stanfordkarel/actions/workflows/test.yml)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/TylerYep/stanfordkarel/main.svg)](https://results.pre-commit.ci/latest/github/TylerYep/stanfordkarel/main)
[![GitHub license](https://img.shields.io/github/license/TylerYep/stanfordkarel)](https://github.com/TylerYep/stanfordkarel/blob/main/LICENSE)
[![Downloads](https://pepy.tech/badge/stanfordkarel)](https://pepy.tech/project/stanfordkarel)

Expand Down Expand Up @@ -116,7 +117,7 @@ The autograde command also runs the builtin Karel Style Checker that performs li

Everything important is located in `stanfordkarel/`.

- Python 3.5+ is required because of `importlib.util.module_from_spec`
- Python 3.5+ is required because of `importlib.util.module_from_spec`.
- Python 3.6+ is required for f-strings.
- Python 3.7+ is required for type annotations.
- `stanfordkarel/` is the exported package, which contains all of the available functions and commands for students to use.
Expand All @@ -126,7 +127,7 @@ Everything important is located in `stanfordkarel/`.

All issues and pull requests are much appreciated!

- First, run `pre-commit install`.
- First, run `pip install pre-commit` and `pre-commit install`.
- To see test coverage scripts and other auto-formatting tools, use `pre-commit run`.
- To run all tests, run `pytest`.

Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ stanfordkarel = py.typed
strict = True
allow_untyped_calls = True
show_error_codes = True
warn_unused_configs = True

[mypy-problems.*]
ignore_errors = True
Expand Down
42 changes: 21 additions & 21 deletions stanfordkarel/karel_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import importlib.util
import inspect
import os
import sys
import tkinter as tk
import traceback as tb
Expand Down Expand Up @@ -46,20 +45,19 @@ def __init__(self, code_file: Path) -> None:
try:
mod = importlib.util.module_from_spec(spec)
self.mods = [mod]
spec.loader.exec_module(mod) # type: ignore
spec.loader.exec_module(mod) # type: ignore[union-attr]
# Go through attributes to find imported modules
for name in dir(mod):
val = getattr(mod, name)
if isinstance(val, ModuleType):
code_file_path = Path(val.__file__)
# Only execute modules outside of this directory
path = os.path.dirname(val.__file__)
dir_path = os.path.dirname(os.path.realpath(__file__))
if path != dir_path:
if code_file_path.parent != Path(__file__).resolve().parent:
self.mods.append(val)
code_file_path = Path(val.__file__)
spec = importlib.util.spec_from_file_location(
name, code_file_path.resolve())
spec.loader.exec_module(val) # type: ignore
name, code_file_path.resolve()
)
spec.loader.exec_module(val) # type: ignore[union-attr]
except SyntaxError as e:
# Handle syntax errors and only print location of error
print(f"Syntax Error: {e}")
Expand All @@ -72,7 +70,7 @@ def __init__(self, code_file: Path) -> None:
sys.exit()

def __repr__(self) -> str:
return '\n'.join([inspect.getsource(mod) for mod in self.mods])
return "\n".join([inspect.getsource(mod) for mod in self.mods])

def inject_namespace(self, karel: KarelProgram) -> None:
"""
Expand Down Expand Up @@ -162,7 +160,6 @@ def load_student_code(self) -> None:
self.student_code.inject_namespace(self.karel)
self.inject_decorator_namespace()


def set_dock_icon(self) -> None:
# make Karel dock icon image
path = Path(__file__).absolute().parent / "icon.png"
Expand Down Expand Up @@ -308,16 +305,19 @@ def inject_decorator_namespace(self) -> None:
in the world.
"""
for mod in self.student_code.mods:
mod.turn_left = self.karel_action_decorator( # type: ignore
self.karel.turn_left)
mod.move = self.karel_action_decorator( # type: ignore
self.karel.move)
mod.pick_beeper = ( # type: ignore
self.beeper_action_decorator(self.karel.pick_beeper))
mod.put_beeper = self.beeper_action_decorator( # type: ignore
self.karel.put_beeper)
mod.paint_corner = ( # type: ignore
self.corner_action_decorator(self.karel.paint_corner))
mod.turn_left = self.karel_action_decorator( # type: ignore[attr-defined]
self.karel.turn_left
)
mod.move = self.karel_action_decorator(self.karel.move) # type: ignore
mod.pick_beeper = self.beeper_action_decorator( # type: ignore
self.karel.pick_beeper
)
mod.put_beeper = self.beeper_action_decorator( # type: ignore
self.karel.put_beeper
)
mod.paint_corner = self.corner_action_decorator( # type: ignore
self.karel.paint_corner
)

def disable_buttons(self) -> None:
self.program_control_button.configure(state="disabled")
Expand Down Expand Up @@ -351,7 +351,7 @@ def run_program(self) -> None:
try:
self.status_label.configure(text="Running...", fg="brown")
self.disable_buttons()
self.student_code.mods[0].main() # type: ignore
self.student_code.mods[0].main() # type: ignore[attr-defined]
self.status_label.configure(text="Finished running.", fg="green")

except (KarelException, NameError) as e:
Expand Down
3 changes: 1 addition & 2 deletions stanfordkarel/karel_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def reset_world(self) -> None:
def reload_world(self, filename: str | None = None) -> None:
"""Reloads world using constructor."""
# TODO fix this
self.__init__(filename) # type: ignore
self.__init__(filename) # type: ignore[misc]

def save_to_file(self, filename: Path) -> None:
# First, output dimensions of world
Expand Down Expand Up @@ -376,7 +376,6 @@ class Direction(Enum):
def __lt__(self, other: object) -> bool:
"""Required to sort Directions."""
if isinstance(other, Direction):
# pylint: disable=comparison-with-callable
return self.value < other.value
return NotImplemented

Expand Down
6 changes: 3 additions & 3 deletions stanfordkarel/style_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def __init__(self, code_file: Path) -> None:
self.module_lines = str(self.student_code).split("\n")
module_member_list = [inspect.getmembers(mod) for mod in self.modules]
self.function_list = [
o[0]
for o in
var_name
for var_name, value in
# flatten module_member_list
[m for module_members in module_member_list for m in module_members]
if inspect.isfunction(o[1])
if inspect.isfunction(value)
]

@staticmethod
Expand Down

0 comments on commit 2bb2dbe

Please sign in to comment.