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

Auto-formatting with black and isort #97

Merged
merged 11 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
- [ ] My change requires a change to the documentation.
- [ ] I have updated the tests accordingly (*required for a bug fix or a new feature*).
- [ ] I have updated the documentation accordingly.
- [ ] I have checked the codestyle using `make lint`
- [ ] I have ensured `make pytest` and `make type` both pass.
- [ ] I have reformatted the code using `make format` (**required**)
- [ ] I have checked the codestyle using `make check-codestyle` and `make lint` (**required**)
- [ ] I have ensured `make pytest` and `make type` both pass. (**required**)


Note: we are using a maximum length of 127 characters per line

<!--- This Template is an edited version of the one from https://github.com/evilsocket/pwnagotchi/ -->
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
- name: Type check
run: |
make type
- name: Check codestyle
run: |
make check-codestyle
- name: Lint with flake8
run: |
make lint
Expand Down
3 changes: 3 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ doc-build:

lint-check:
script:
# TODO: remove when the docker image is updated
- pip install black isort
- make check-codestyle
- make lint
27 changes: 7 additions & 20 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,9 @@ pip install -e .[docs,tests,extra]

## Codestyle

We follow the [PEP8 codestyle](https://www.python.org/dev/peps/pep-0008/). Please order the imports as follows:
We are using [black codestyle](https://github.com/psf/black) (max line length of 127 characters) together with [isort](https://github.com/timothycrosley/isort) to sort the imports.

1. built-in
2. packages
3. current module

with one space between each, that gives for instance:
```python
import os
import warnings

import numpy as np

from stable_baselines3 import PPO
```

In general, we recommend using pycharm to format everything in an efficient way.
**Please run `make format`** to reformat your code. You can check the codestyle using `make check-codestyle` and `make lint`.
araffin marked this conversation as resolved.
Show resolved Hide resolved

Please document each function/method and [type](https://google.github.io/pytype/user_guide.html) them using the following template:

Expand All @@ -77,11 +63,10 @@ def my_function(arg1: type1, arg2: type2) -> returntype:
Before proposing a PR, please open an issue, where the feature will be discussed. This prevent from duplicated PR to be proposed and also ease the code review process.

Each PR need to be reviewed and accepted by at least one of the maintainers (@hill-a, @araffin, @erniejunior, @AdamGleave or @Miffyli).
A PR must pass the Continuous Integration tests (travis + codacy) to be merged with the master branch.
A PR must pass the Continuous Integration tests to be merged with the master branch.

Note: in rare cases, we can create exception for codacy failure.

## Test
## Tests

All new features must add tests in the `tests/` folder ensuring that everything works fine.
We use [pytest](https://pytest.org/).
Expand All @@ -99,9 +84,10 @@ Type checking with `pytype`:
make type
```

Codestyle check with `flake8`:
Codestyle check with `black`, `isort` and `flake8`:

```
make check-codestyle
make lint
```

Expand All @@ -121,6 +107,7 @@ make spelling
## Changelog and Documentation

Please do not forget to update the changelog (`docs/misc/changelog.rst`) and add documentation if needed.
You should add your username next to each changelog entry that you added. If this is your first contribution, please add your username at the bottom too.
A README is present in the `docs/` folder for instructions on how to build the documentation.


Expand Down
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ lint:
# exit-zero treats all errors as warnings.
flake8 ${LINT_PATHS} --count --exit-zero --statistics

format:
# Sort imports
isort ${LINT_PATHS}
# Reformat using black
black -l 127 ${LINT_PATHS}

check-codestyle:
# Sort imports
isort --check ${LINT_PATHS}
# Reformat using black
black --check -l 127 ${LINT_PATHS}

doc:
cd docs && make html

Expand All @@ -23,8 +35,6 @@ spelling:
clean:
cd docs && make clean

.PHONY: clean spelling doc lint

# Build docker images
# If you do export RELEASE=True, it will also push them
docker: docker-cpu docker-gpu
Expand All @@ -46,3 +56,5 @@ test-release:
python setup.py sdist
python setup.py bdist_wheel
twine upload --repository-url https://test.pypi.org/legacy/ dist/*

.PHONY: clean spelling doc lint format check-codestyle
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<img src="docs/\_static/img/logo.png" align="right" width="40%"/>

[![pipeline status](https://gitlab.com/araffin/stable-baselines3/badges/master/pipeline.svg)](https://gitlab.com/araffin/stable-baselines3/-/commits/master) [![Documentation Status](https://readthedocs.org/projects/stable-baselines/badge/?version=master)](https://stable-baselines3.readthedocs.io/en/master/?badge=master) [![coverage report](https://gitlab.com/araffin/stable-baselines3/badges/master/coverage.svg)](https://gitlab.com/araffin/stable-baselines3/-/commits/master)

[![codestyle](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

**WARNING: Stable Baselines3 is currently in a beta version, breaking changes may occur before 1.0 is released**

Expand Down
72 changes: 36 additions & 36 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
# PyEnchant.
try:
import sphinxcontrib.spelling # noqa: F401

enable_spell_check = True
except ImportError:
enable_spell_check = False

# source code directory, relative to this file, for sphinx-autobuild
sys.path.insert(0, os.path.abspath('..'))
sys.path.insert(0, os.path.abspath(".."))


class Mock(MagicMock):
Expand All @@ -44,18 +45,18 @@ def __getattr__(cls, name):
sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES)

# Read version from file
version_file = os.path.join(os.path.dirname(__file__), '../stable_baselines3', 'version.txt')
with open(version_file, 'r') as file_handler:
version_file = os.path.join(os.path.dirname(__file__), "../stable_baselines3", "version.txt")
with open(version_file, "r") as file_handler:
__version__ = file_handler.read().strip()

# -- Project information -----------------------------------------------------

project = 'Stable Baselines3'
copyright = '2020, Stable Baselines3'
author = 'Stable Baselines3 Contributors'
project = "Stable Baselines3"
copyright = "2020, Stable Baselines3"
author = "Stable Baselines3 Contributors"

# The short X.Y version
version = 'master (' + __version__ + ' )'
version = "master (" + __version__ + " )"
# The full version, including alpha/beta/rc tags
release = __version__

Expand All @@ -70,30 +71,30 @@ def __getattr__(cls, name):
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
"sphinx.ext.autodoc",
# 'sphinx_autodoc_typehints',
'sphinx.ext.autosummary',
'sphinx.ext.mathjax',
'sphinx.ext.ifconfig',
'sphinx.ext.viewcode',
"sphinx.ext.autosummary",
"sphinx.ext.mathjax",
"sphinx.ext.ifconfig",
"sphinx.ext.viewcode",
# 'sphinx.ext.intersphinx',
# 'sphinx.ext.doctest'
]

if enable_spell_check:
extensions.append('sphinxcontrib.spelling')
extensions.append("sphinxcontrib.spelling")

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'
source_suffix = ".rst"

# The master toctree document.
master_doc = 'index'
master_doc = "index"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand All @@ -105,10 +106,10 @@ def __getattr__(cls, name):
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path .
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
pygments_style = "sphinx"


# -- Options for HTML output -------------------------------------------------
Expand All @@ -117,13 +118,13 @@ def __getattr__(cls, name):
# a list of builtin themes.

# Fix for read the docs
on_rtd = os.environ.get('READTHEDOCS') == 'True'
on_rtd = os.environ.get("READTHEDOCS") == "True"
if on_rtd:
html_theme = 'default'
html_theme = "default"
else:
html_theme = 'sphinx_rtd_theme'
html_theme = "sphinx_rtd_theme"

html_logo = '_static/img/logo.png'
html_logo = "_static/img/logo.png"


def setup(app):
Expand All @@ -139,7 +140,7 @@ def setup(app):
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
Expand All @@ -155,7 +156,7 @@ def setup(app):
# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'StableBaselines3doc'
htmlhelp_basename = "StableBaselines3doc"


# -- Options for LaTeX output ------------------------------------------------
Expand All @@ -164,15 +165,12 @@ def setup(app):
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',

# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
Expand All @@ -182,19 +180,15 @@ def setup(app):
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'StableBaselines3.tex', 'Stable Baselines3 Documentation',
'Stable Baselines3 Contributors', 'manual'),
(master_doc, "StableBaselines3.tex", "Stable Baselines3 Documentation", "Stable Baselines3 Contributors", "manual"),
]


# -- Options for manual page output ------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'stablebaselines3', 'Stable Baselines3 Documentation',
[author], 1)
]
man_pages = [(master_doc, "stablebaselines3", "Stable Baselines3 Documentation", [author], 1)]


# -- Options for Texinfo output ----------------------------------------------
Expand All @@ -203,9 +197,15 @@ def setup(app):
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'StableBaselines3', 'Stable Baselines3 Documentation',
author, 'StableBaselines3', 'One line description of project.',
'Miscellaneous'),
(
master_doc,
"StableBaselines3",
"Stable Baselines3 Documentation",
author,
"StableBaselines3",
"One line description of project.",
"Miscellaneous",
),
]


Expand Down
1 change: 1 addition & 0 deletions docs/misc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Others:
- Split the ``collect_rollout()`` method for off-policy algorithms
- Added ``_on_step()`` for off-policy base class
- Optimized replay buffer size by removing the need of ``next_observations`` numpy array
- Switch to ``black`` codestyle and added ``make format`` and ``make check-codestyle``

Documentation:
^^^^^^^^^^^^^^
Expand Down
7 changes: 6 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ filterwarnings =
inputs = stable_baselines3

[flake8]
ignore = W503,W504 # line breaks before and after binary operators
ignore = W503,W504,E203,E231 # line breaks before and after binary operators
# Ignore import not used when aliases are defined
per-file-ignores =
./stable_baselines3/__init__.py:F401
Expand All @@ -47,3 +47,8 @@ exclude =
max-complexity = 15
# The GitHub editor is 127 chars wide
max-line-length = 127

[isort]
profile = black
line_length = 127
src_paths = stable_baselines3