Skip to content

Commit

Permalink
refactor: Improved project code quality (#53)
Browse files Browse the repository at this point in the history
* test: Switched to pytest

* docs: Updated documentation

* ci: Updated CI jobs

* style: Updated import order

* style: Updated import order

* refactor: Refactored setup

* feat: Added isort, pydocstyle and contribution utils

* style: Fixed lint

* chore: Fixed documentation reqs
  • Loading branch information
frgfm authored May 18, 2022
1 parent 358edb6 commit 4d4fcbf
Show file tree
Hide file tree
Showing 46 changed files with 1,510 additions and 550 deletions.
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[flake8]
max-line-length = 120
ignore = F401, E402, E265, F403, W503, W504, F821, W605
ignore = E402, E265, F403, W503, W504, E731
exclude = .github, .git, venv*, docs, build
per-file-ignores = **/__init__.py:F401
45 changes: 0 additions & 45 deletions .github/ISSUE_TEMPLATE/bug_report.md

This file was deleted.

64 changes: 64 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: 🐛 Bug report
description: Create a report to help us improve the library
labels: 'type: bug'
assignees: frgfm

body:
- type: markdown
attributes:
value: >
#### Before reporting a bug, please check that the issue hasn't already been addressed in [the existing and past issues](https://github.com/frgfm/torch-cam/issues?q=is%3Aissue).
- type: textarea
attributes:
label: Bug description
description: |
A clear and concise description of what the bug is.
Please explain the result you observed and the behavior you were expecting.
placeholder: |
A clear and concise description of what the bug is.
validations:
required: true

- type: textarea
attributes:
label: Code snippet to reproduce the bug
description: |
Sample code to reproduce the problem.
Please wrap your code snippet with ```` ```triple quotes blocks``` ```` for readability.
placeholder: |
```python
Sample code to reproduce the problem
```
validations:
required: true
- type: textarea
attributes:
label: Error traceback
description: |
The error message you received running the code snippet, with the full traceback.
Please wrap your error message with ```` ```triple quotes blocks``` ```` for readability.
placeholder: |
```
The error message you got, with the full traceback.
```
validations:
required: true
- type: textarea
attributes:
label: Environment
description: |
Please run the following command and paste the output below.
```sh
wget https://raw.githubusercontent.com/frgfm/torch-scan/master/scripts/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
```
validations:
required: true
- type: markdown
attributes:
value: >
Thanks for helping us improve the library!
5 changes: 5 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
blank_issues_enabled: true
contact_links:
- name: Usage questions
url: https://github.com/frgfm/torch-scan/discussions
about: Ask questions and discuss with other TorchCAM community members
27 changes: 0 additions & 27 deletions .github/ISSUE_TEMPLATE/feature_request.md

This file was deleted.

34 changes: 34 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: 🚀 Feature request
description: Submit a proposal/request for a new feature
labels: 'type: enhancement'
assignees: frgfm

body:
- type: textarea
attributes:
label: 🚀 Feature
description: >
A clear and concise description of the feature proposal
validations:
required: true
- type: textarea
attributes:
label: Motivation & pitch
description: >
Please outline the motivation for the proposal. Is your feature request related to a specific problem? e.g., *"I'm working on X and would like Y to be possible"*. If this is related to another GitHub issue, please link here too.
validations:
required: true
- type: textarea
attributes:
label: Alternatives
description: >
A description of any alternative solutions or features you've considered, if any.
- type: textarea
attributes:
label: Additional context
description: >
Add any other context or screenshots about the feature request.
- type: markdown
attributes:
value: >
Thanks for contributing 🎉
20 changes: 20 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# What does this PR do?

<!--
Well, hello there! Thank you for proposing modifications to the project.
Make sure to have both a short descriptive title & explain your modifications with the relevant context. Make sure to include reference to Github issues it is related to. For the sake of keeping the library light, if you modified existing dependencies or added new ones, please state it clearly in your description.
-->

<!-- Remove if not applicable -->

Closes # (issue)


## Before submitting
- [ ] Was this discussed/approved in a Github [issue](https://github.com/frgfm/torch-scan/issues?q=is%3Aissue) or a [discussion](https://github.com/frgfm/torch-scan/discussions)? Please add a link to it if that's the case.
- [ ] You have read the [contribution guidelines](https://github.com/frgfm/torch-scan/blob/master/CONTRIBUTING.md#submitting-a-pull-request) and followed them in this PR.
- [ ] Did you make sure to update the documentation with your changes? Here are the
[documentation guidelines](https://github.com/frgm/torch-scan/tree/master/docs).
- [ ] Did you write any new necessary tests?
24 changes: 24 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
changelog:
exclude:
labels:
- ignore-for-release
categories:
- title: Breaking Changes 🛠
labels:
- "type: breaking change"
# NEW FEATURES
- title: New Features 🚀
labels:
- "type: new feature"
# BUG FIXES
- title: Bug Fixes 🐛
labels:
- "type: bug"
# IMPROVEMENTS
- title: Improvements
labels:
- "type: enhancement"
# MISC
- title: Miscellaneous
labels:
- "type: misc"
64 changes: 64 additions & 0 deletions .github/validate_deps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from pathlib import Path

import requirements
from requirements.requirement import Requirement

# Deps that won't have a specific requirements.txt
IGNORE = ["flake8", "isort", "mypy", "pydocstyle"]
# All req files to check
REQ_FILES = ["requirements.txt", "tests/requirements.txt", "docs/requirements.txt"]


def main():

# Collect the deps from all requirements.txt
folder = Path(__file__).parent.parent.absolute()
req_deps = {}
for file in REQ_FILES:
with open(folder.joinpath(file), 'r') as f:
_deps = [(req.name, req.specs) for req in requirements.parse(f)]

for _dep in _deps:
lib, specs = _dep
assert req_deps.get(lib, specs) == specs, f"conflicting deps for {lib}"
req_deps[lib] = specs

# Collect the one from setup.py
setup_deps = {}
with open(folder.joinpath("setup.py"), 'r') as f:
setup = f.readlines()
lines = setup[setup.index("_deps = [\n") + 1:]
lines = [_dep.strip() for _dep in lines[:lines.index("]\n")]]
lines = [_dep.split('"')[1] for _dep in lines if _dep.startswith('"')]
_reqs = [Requirement.parse(_line) for _line in lines]
_deps = [(req.name, req.specs) for req in _reqs]
for _dep in _deps:
lib, specs = _dep
assert setup_deps.get(lib) is None, f"conflicting deps for {lib}"
setup_deps[lib] = specs

# Remove ignores
for k in IGNORE:
if isinstance(req_deps.get(k), list):
del req_deps[k]
if isinstance(setup_deps.get(k), list):
del setup_deps[k]

# Compare them
assert len(req_deps) == len(setup_deps)
mismatches = []
for k, v in setup_deps.items():
assert isinstance(req_deps.get(k), list)
if req_deps[k] != v:
mismatches.append((k, v, req_deps[k]))

if len(mismatches) > 0:
mismatch_str = "version specifiers mismatches:\n"
mismatch_str += '\n'.join(
f"- {lib}: {setup} (from setup.py) | {reqs} (from requirements)"
for lib, setup, reqs in mismatches
)
raise AssertionError(mismatch_str)

if __name__ == "__main__":
main()
64 changes: 64 additions & 0 deletions .github/validate_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from datetime import datetime
from pathlib import Path

shebang = ["#!usr/bin/python\n"]
blank_line = "\n"

# Possible years
starting_year = 2020
current_year = datetime.now().year

year_options = [f"{current_year}"] + [f"{year}-{current_year}" for year in range(starting_year, current_year)]
copyright_notices = [
[f"# Copyright (C) {year_str}, François-Guillaume Fernandez.\n"]
for year_str in year_options
]
license_notice = [
"# This program is licensed under the Apache License version 2.\n",
"# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details.\n"
]

# Define all header options
HEADERS = [
shebang + [blank_line] + copyright_notice + [blank_line] + license_notice
for copyright_notice in copyright_notices
] + [
copyright_notice + [blank_line] + license_notice
for copyright_notice in copyright_notices
]


IGNORED_FILES = ["version.py", "__init__.py"]
FOLDERS = ["torchscan", "scripts"]


def main():

invalid_files = []

# For every python file in the repository
for folder in FOLDERS:
for source_path in Path(__file__).parent.parent.joinpath(folder).rglob('**/*.py'):
if source_path.name not in IGNORED_FILES:
# Parse header
header_length = max(len(option) for option in HEADERS)
current_header = []
with open(source_path) as f:
for idx, line in enumerate(f):
current_header.append(line)
if idx == header_length - 1:
break
# Validate it
if not any(
"".join(current_header[:min(len(option), len(current_header))]) == "".join(option)
for option in HEADERS
):
invalid_files.append(source_path)

if len(invalid_files) > 0:
invalid_str = "\n- " + "\n- ".join(map(str, invalid_files))
raise AssertionError(f"Invalid header in the following files:{invalid_str}")


if __name__ == "__main__":
main()
Loading

0 comments on commit 4d4fcbf

Please sign in to comment.