Skip to content

Commit

Permalink
chore: adds mypy type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Dec 2, 2021
1 parent 41bc8de commit d2aab9c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ jobs:
python-version: "3.10"
- name: Install Dependencies
run: make install
- name: Run linting
run: make lint
- name: Check format
run: make format-check
- name: Check imports
run: make isort-check
test:
runs-on: ubuntu-latest
strategy:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 2.0.2 (2021-12-02)

* Adds `mypy` type checking and fixes types
* Fixes a potential bug when regex pattern matching on package names would result in `None`

## v2.0.1 (2021-11-25)

* Small bug fix to change the types of functions available in the `__all__` variable to strings
Expand Down
21 changes: 13 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
PYTHON_BINARY := python3
VIRTUAL_BIN := venv/bin
PROJECT_NAME := pip_tree
TEST_DIR := test

## help - Display help about make targets for this Makefile
help:
Expand All @@ -24,17 +25,17 @@ clean:

## black - Runs the Black Python formatter against the project
black:
$(VIRTUAL_BIN)/black $(PROJECT_NAME)/ test/
$(VIRTUAL_BIN)/black $(PROJECT_NAME)/ $(TEST_DIR)/

## black-check - Checks if the project is formatted correctly against the Black rules
black-check:
$(VIRTUAL_BIN)/black $(PROJECT_NAME)/ test/ --check
$(VIRTUAL_BIN)/black $(PROJECT_NAME)/ $(TEST_DIR)/ --check

## format - Runs all formatting tools against the project
format: black isort lint
format: black isort lint mypy

## format-check - Checks if the project is formatted correctly against all formatting rules
format-check: black-check isort-check lint
format-check: black-check isort-check lint mypy

## install - Install the project locally
install:
Expand All @@ -44,18 +45,22 @@ install:

## isort - Sorts imports throughout the project
isort:
$(VIRTUAL_BIN)/isort $(PROJECT_NAME)/ test/
$(VIRTUAL_BIN)/isort $(PROJECT_NAME)/ $(TEST_DIR)/

## isort-check - Checks that imports throughout the project are sorted correctly
isort-check:
$(VIRTUAL_BIN)/isort $(PROJECT_NAME)/ test/ --check-only
$(VIRTUAL_BIN)/isort $(PROJECT_NAME)/ $(TEST_DIR)/ --check-only

## lint - Lint the project
lint:
$(VIRTUAL_BIN)/flake8 $(PROJECT_NAME)/ test/
$(VIRTUAL_BIN)/flake8 $(PROJECT_NAME)/ $(TEST_DIR)/

## mypy - Run mypy type checking on the project
mypy:
$(VIRTUAL_BIN)/mypy $(PROJECT_NAME)/ $(TEST_DIR)/

## test - Test the project
test:
$(VIRTUAL_BIN)/pytest

.PHONY: help build coverage clean black black-check format format-check install isort isort-check lint test
.PHONY: help build coverage clean black black-check format format-check install isort isort-check lint mypy test
Empty file added pip_tree/py.typed
Empty file.
18 changes: 11 additions & 7 deletions pip_tree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
import os
import re
import time
from typing import Dict, List, Tuple
from typing import Any, Dict, Generator, List, Tuple

import pkg_resources


def generate_pip_tree(path: str) -> Tuple[Dict, int]:
def generate_pip_tree(path: str) -> Tuple[List[Dict[str, Any]], int]:
"""Generate the Pip Tree of the virtual environment specified."""
pip_tree_results = []
required_by_data = {}
required_by_data: Dict[str, List[str]] = {}
package_count = 0

packages = get_pip_package_list(path)
Expand All @@ -29,7 +29,7 @@ def generate_pip_tree(path: str) -> Tuple[Dict, int]:
return final_output, package_count


def get_pip_package_list(path: str) -> List[pkg_resources.DistInfoDistribution]:
def get_pip_package_list(path: str) -> Generator[pkg_resources.Distribution, None, None]:
"""Get the Pip package list of a Python virtual environment.
Must be a path like: /project/venv/lib/python3.9/site-packages
Expand All @@ -39,7 +39,7 @@ def get_pip_package_list(path: str) -> List[pkg_resources.DistInfoDistribution]:
return packages


def get_package_details(package: pkg_resources.DistInfoDistribution) -> Dict:
def get_package_details(package: pkg_resources.Distribution) -> Dict[str, Any]:
"""Build a dictionary of details for a package from Pip."""
package_updated_at = time.ctime(os.path.getctime(package.location))
requires_list = [sorted(str(requirement) for requirement in package.requires())]
Expand All @@ -54,14 +54,18 @@ def get_package_details(package: pkg_resources.DistInfoDistribution) -> Dict:
return package_details


def _generate_reverse_requires_field(required_by_data: Dict, package_details: Dict):
def _generate_reverse_requires_field(required_by_data: Dict[str, List[str]], package_details: Dict[str, Any]):
"""Generate a reversed list from the `requires` fields and create a
collection of each `required_by` fields so each package can show what it's required by.
"""
requires_list = [item for item in package_details['requires']]
for required_by_package in requires_list:
word = re.compile(r'^(\w)+')
required_by_package_name = word.match(required_by_package).group()
name_match = word.match(required_by_package)
if name_match is not None:
required_by_package_name = name_match.group()
else:
required_by_package_name = ''

# If a package is listed, append to it, otherwise create a new list
if required_by_data.get(required_by_package_name):
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
'coveralls == 3.*',
'flake8',
'isort',
'mypy',
'pytest == 6.*',
'pytest-cov == 2.*',
'types-setuptools',
]

setuptools.setup(
name='pip-tree',
version='2.0.1',
version='2.0.2',
description='Get the dependency tree of your Python virtual environment via Pip.',
long_description=long_description,
long_description_content_type="text/markdown",
url='http://github.com/justintime50/pip-tree',
author='Justintime50',
license='MIT',
packages=setuptools.find_packages(),
package_data={'pip_tree': ['py.typed']},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down

0 comments on commit d2aab9c

Please sign in to comment.