Skip to content

Commit

Permalink
Merge pull request #417 from grimley517/#415-linter-preventing-prs
Browse files Browse the repository at this point in the history
#415 linter preventing prs
  • Loading branch information
faif committed May 18, 2024
2 parents d4b7f97 + 9581e5a commit 81e78fe
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 53 deletions.
15 changes: 3 additions & 12 deletions .github/workflows/lint_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: 3.x
- run: pip install --upgrade pip
- run: pip install black codespell flake8 isort mypy pytest pyupgrade tox
- run: black --check .
- run: codespell --quiet-level=2 # --ignore-words-list="" --skip=""
- run: flake8 . --count --show-source --statistics
- run: isort --profile black .
- run: tox
- run: pip install -e .
- run: mypy --ignore-missing-imports . || true
- run: pytest .
- run: pytest --doctest-modules . || true
- run: shopt -s globstar && pyupgrade --py37-plus **/*.py
- shell: bash
name: Lint and test
run: ./lint.sh
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ venv/
.vscode/
.python-version
.coverage
build/
dist/
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ To see Python 2 compatible versions of some patterns please check-out the [legac
When everything else is done - update corresponding part of README.

##### Travis CI
Please run `tox` or `tox -e ci37` before submitting a patch to be sure your changes will pass CI.
Please run the following before submitting a patch
- `black .` This lints your code.

Then either:
- `tox` or `tox -e ci37` This runs unit tests. see tox.ini for further details.
- If you have a bash compatible shell use `./lint.sh` This script will lint and test your code. This script mirrors the CI pipeline actions.

You can also run `flake8` or `pytest` commands manually. Examples can be found in `tox.ini`.

Expand Down
14 changes: 14 additions & 0 deletions lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /bin/bash

pip install --upgrade pip
pip install black codespell flake8 isort mypy pytest pyupgrade tox
black --check .
codespell --quiet-level=2 # --ignore-words-list="" --skip=""
flake8 . --count --show-source --statistics
isort --profile black .
tox
pip install -e .
mypy --ignore-missing-imports . || true
pytest .
pytest --doctest-modules . || true
shopt -s globstar && pyupgrade --py37-plus **/*.py
3 changes: 0 additions & 3 deletions patterns/behavioral/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def main_method(self) -> None:

# Alternative implementation for different levels of methods
class CatalogInstance:

"""catalog of multiple methods that are executed depending on an init
parameter
Expand Down Expand Up @@ -82,7 +81,6 @@ def main_method(self) -> None:


class CatalogClass:

"""catalog of multiple class methods that are executed depending on an init
parameter
Expand Down Expand Up @@ -121,7 +119,6 @@ def main_method(self):


class CatalogStatic:

"""catalog of multiple static methods that are executed depending on an init
parameter
Expand Down
1 change: 1 addition & 0 deletions patterns/behavioral/iterator_alt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*TL;DR
Traverses a container and accesses the container's elements.
"""

from __future__ import annotations


Expand Down
2 changes: 1 addition & 1 deletion patterns/behavioral/memento.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Provides the ability to restore an object to its previous state.
"""

from typing import Callable, List
from copy import copy, deepcopy
from typing import Callable, List


def memento(obj, deep=False):
Expand Down
1 change: 0 additions & 1 deletion patterns/behavioral/publish_subscribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
Author: https://github.com/HanWenfang
"""


from __future__ import annotations


Expand Down
4 changes: 2 additions & 2 deletions patterns/behavioral/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Enables selecting an algorithm at runtime.
"""


from __future__ import annotations

from typing import Callable
Expand Down Expand Up @@ -56,7 +55,8 @@ def apply_discount(self) -> float:
return self.price - discount

def __repr__(self) -> str:
return f"<Order price: {self.price} with discount strategy: {getattr(self.discount_strategy,'__name__',None)}>"
strategy = getattr(self.discount_strategy, "__name__", None)
return f"<Order price: {self.price} with discount strategy: {strategy}>"


def ten_percent_discount(order: Order) -> float:
Expand Down
26 changes: 0 additions & 26 deletions patterns/creational/abstract_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def __str__(self) -> str:


class PetShop:

"""A pet shop"""

def __init__(self, animal_factory: Type[Pet]) -> None:
Expand All @@ -78,14 +77,6 @@ def buy_pet(self, name: str) -> Pet:
return pet


# Additional factories:

# Create a random animal
def random_animal(name: str) -> Pet:
"""Let's be dynamic!"""
return random.choice([Dog, Cat])(name)


# Show pets with various factories
def main() -> None:
"""
Expand All @@ -95,27 +86,10 @@ def main() -> None:
Here is your lovely Cat<Lucy>
>>> pet.speak()
meow
# A shop that sells random animals
>>> shop = PetShop(random_animal)
>>> for name in ["Max", "Jack", "Buddy"]:
... pet = shop.buy_pet(name)
... pet.speak()
... print("=" * 20)
Here is your lovely Cat<Max>
meow
====================
Here is your lovely Dog<Jack>
woof
====================
Here is your lovely Dog<Buddy>
woof
====================
"""


if __name__ == "__main__":
random.seed(1234) # for deterministic doctest outputs
shop = PetShop(random_animal)
import doctest

Expand Down
1 change: 1 addition & 0 deletions patterns/creational/borg.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*TL;DR
Provides singleton-like behavior sharing state between instances.
"""

from typing import Dict


Expand Down
6 changes: 2 additions & 4 deletions patterns/creational/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
*TL;DR
Creates objects without having to specify the exact class.
"""
from typing import Dict
from typing import Protocol
from typing import Type

from typing import Dict, Protocol, Type


class Localizer(Protocol):
Expand All @@ -50,7 +49,6 @@ def localize(self, msg: str) -> str:


def get_localizer(language: str = "English") -> Localizer:

"""Factory"""
localizers: Dict[str, Type[Localizer]] = {
"English": EnglishLocalizer,
Expand Down
1 change: 1 addition & 0 deletions patterns/creational/prototype.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*TL;DR
Creates new object instances by cloning prototype.
"""

from __future__ import annotations

from typing import Any
Expand Down
1 change: 1 addition & 0 deletions patterns/other/blackboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
https://en.wikipedia.org/wiki/Blackboard_system
"""

from __future__ import annotations

import abc
Expand Down
1 change: 0 additions & 1 deletion patterns/other/graph_search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class GraphSearch:

"""Graph search emulation in python, from source
http://www.python.org/doc/essays/graphs/
Expand Down
2 changes: 1 addition & 1 deletion tests/behavioral/test_strategy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from patterns.behavioral.strategy import Order, ten_percent_discount, on_sale_discount
from patterns.behavioral.strategy import Order, on_sale_discount, ten_percent_discount


@pytest.fixture
Expand Down
1 change: 0 additions & 1 deletion tests/creational/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def test_frozen_pool(self):


class TestNaitivePool(unittest.TestCase):

"""def test_object(queue):
queue_object = QueueObject(queue, True)
print('Inside func: {}'.format(queue_object.object))"""
Expand Down

0 comments on commit 81e78fe

Please sign in to comment.