Skip to content

Commit

Permalink
Merge pull request #122 from brandonwillard/fix-imports-style
Browse files Browse the repository at this point in the history
- Use `isort` to sort imports
- Remove import cycle in `theano.gpuarray`
- Remove some Python 2.x code using `pyupgrade`.
  • Loading branch information
brandonwillard committed Oct 22, 2020
2 parents ead9e37 + 008579b commit 8e8eda2
Show file tree
Hide file tree
Showing 320 changed files with 4,392 additions and 5,046 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Expand Up @@ -48,10 +48,11 @@ jobs:
- name: Install dependencies
run: |
python -m pip install -U pip
python -m pip install -U black flake8
python -m pip install -U black flake8 isort
- name: Check the style
run: |
black -t py36 --check theano/ tests/ setup.py
black -t py36 --check theano/ tests/ setup.py conftest.py
isort --check theano/ tests/ setup.py conftest.py
flake8
test:
Expand Down
26 changes: 25 additions & 1 deletion .pre-commit-config.yaml
Expand Up @@ -4,8 +4,32 @@ repos:
hooks:
- id: black
language_version: python3

exclude: |
(?x)^(
versioneer\.py|
theano/_version\.py|
doc/.*|
bin/.*
)$
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
exclude: |
(?x)^(
versioneer\.py|
theano/_version\.py|
doc/.*|
bin/.*
)$
- repo: https://github.com/pycqa/isort
rev: 5.5.2
hooks:
- id: isort
exclude: |
(?x)^(
versioneer\.py|
theano/_version\.py|
doc/.*|
bin/.*
)$
32 changes: 17 additions & 15 deletions Makefile
@@ -1,4 +1,4 @@
.PHONY: help venv conda docker docstyle format style black test lint check coverage pypi
.PHONY: help venv conda docker check-docstyle check-format check-style format style test lint check coverage pypi
.DEFAULT_GOAL = help

PROJECT_NAME = theano
Expand Down Expand Up @@ -35,23 +35,25 @@ venv: # Set up a Python virtual environment for development.
)
@printf "\n\nVirtual environment created! \033[1;34mRun \`source ${PROJECT_NAME}-venv/bin/activate\` to activate it.\033[0m\n\n\n"

docstyle:
@printf "Checking documentation with pydocstyle...\n"
check-docstyle:
@printf "Checking documentation style...\n"
pydocstyle ${PROJECT_DIR}
@printf "\033[1;34mPydocstyle passes!\033[0m\n\n"
@printf "\033[1;34mDocumentation style passes!\033[0m\n\n"

format:
@printf "Checking code format with black...\n"
black -t py36 --check ${PROJECT_DIR} tests/ setup.py conftest.py
@printf "\033[1;34mBlack passes!\033[0m\n\n"
check-format:
@printf "Checking code format...\n"
black -t py36 --check ${PROJECT_DIR} tests/ setup.py conftest.py; \
isort --check ${PROJECT_DIR} tests/ setup.py conftest.py;
@printf "\033[1;34mFormatting passes!\033[0m\n\n"

style:
@printf "Checking code style with pylint...\n"
check-style:
@printf "Checking code style...\n"
flake8
@printf "\033[1;34mPylint passes!\033[0m\n\n"
@printf "\033[1;34mCode style passes!\033[0m\n\n"

black: # Format code in-place using black.
black ${PROJECT_DIR} tests/ setup.py conftest.py
format: # Format code in-place using black.
black ${PROJECT_DIR} tests/ setup.py conftest.py; \
isort ${PROJECT_DIR} tests/ setup.py conftest.py;

test: # Test code using pytest.
pytest -v tests/ ${PROJECT_DIR} --cov=${PROJECT_DIR} --cov-report=xml --html=testing-report.html --self-contained-html
Expand All @@ -65,6 +67,6 @@ pypi:
${PYTHON} setup.py sdist bdist_wheel; \
twine upload --skip-existing dist/*;

lint: docstyle format style # Lint code using pydocstyle, black and pylint.
lint: check-format check-style check-docstyle

check: lint test coverage # Both lint and test code. Runs `make lint` followed by `make test`.
check: lint test coverage
2 changes: 1 addition & 1 deletion doc/dev_start_guide.txt
Expand Up @@ -126,7 +126,7 @@ To setup VIM:

#. Install flake8 (if not already installed) with::

pip install "flake8<3"
pip install flake8

.. warning:: Starting version 3.0.0, flake8 changed its dependencies and
moved its Python API to a legacy module, breaking Theano's flake8 tests.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -14,3 +14,4 @@ jax; python_version > '3.6'
jaxlib; python_version > '3.6'
diff-cover
pre-commit
isort
22 changes: 19 additions & 3 deletions setup.cfg
@@ -1,6 +1,9 @@
[flake8]
ignore=E501,E123,E133,FI12,FI14,FI15,FI50,FI51,FI53,W503,W504,E203,C901,E231,E741
select = C,E,F,W
ignore = E203,E231,E501,E741,W503,W504,C901
max-line-length = 88
per-file-ignores =
**/__init__.py:F401,E402,F403
theano/sparse/sandbox/sp2.py:F401
tests/sparse/test_basic.py:E402
tests/sparse/test_opt.py:E402
Expand All @@ -17,7 +20,6 @@ per-file-ignores =
exclude =
versioneer.py
doc/
__init__.py
theano/_version.py

[versioneer]
Expand All @@ -29,4 +31,18 @@ tag_prefix = rel-

[tool:pytest]
addopts = --durations=50
testpaths = tests/
testpaths = tests/

[pylint]
max-line-length = 88

[pylint.messages_control]
disable = C0330, C0326

[isort]
profile = black
lines_after_imports = 2
lines_between_sections = 1
honor_noqa = True
skip_gitignore = True
skip = theano/version.py
10 changes: 6 additions & 4 deletions setup.py
Expand Up @@ -5,10 +5,11 @@
# * Add download_url


import os
import codecs
from fnmatch import fnmatchcase
import os
from distutils.util import convert_path
from fnmatch import fnmatchcase


try:
from setuptools import setup
Expand All @@ -17,6 +18,7 @@

import versioneer


CLASSIFIERS = """\
Development Status :: 6 - Mature
Intended Audience :: Education
Expand Down Expand Up @@ -82,12 +84,12 @@ def find_packages(where=".", exclude=()):
# Get the fallback version
# We can't import theano.version as it isn't yet installed, so parse it.
fname = os.path.join(os.path.split(__file__)[0], "theano", "version.py")
with open(fname, "r") as f:
with open(fname) as f:
lines = f.readlines()
lines = [l for l in lines if l.startswith("FALLBACK_VERSION")]
assert len(lines) == 1

FALLBACK_VERSION = lines[0].split("=")[1].strip().strip('""')
FALLBACK_VERSION = lines[0].split("=")[1].strip().strip('"')

version_data["version"] = FALLBACK_VERSION

Expand Down
14 changes: 5 additions & 9 deletions tests/compile/test_builders.py
@@ -1,21 +1,17 @@
from functools import partial

import numpy as np
import pytest

import theano
import theano.tensor as tt

from tests import unittest_tools
from theano import config, shared

from theano.gradient import DisconnectedType
from theano.gof.null_type import NullType
from theano.compile import function

from theano.tensor.shared_randomstreams import RandomStreams

from theano.compile.builders import OpFromGraph

from tests import unittest_tools
from theano.gof.null_type import NullType
from theano.gradient import DisconnectedType
from theano.tensor.shared_randomstreams import RandomStreams


class TestOpFromGraph(unittest_tools.InferShapeTester):
Expand Down
12 changes: 3 additions & 9 deletions tests/compile/test_debugmode.py
@@ -1,19 +1,13 @@
import sys

import numpy as np

import pytest

import theano
import theano.tensor

from six import reraise

from theano import config
from theano import gof
from theano.compile import debugmode

from tests import unittest_tools as utt
from theano import config, gof
from theano.compile import debugmode


def test_debugmode_basic():
Expand Down Expand Up @@ -306,7 +300,7 @@ def insert_bad_dtype(node):
new_e = e.__class__("TTT" + str(e))
exc_type, exc_value, exc_trace = sys.exc_info()
exc_value = new_e
reraise(e.__class__, exc_value, exc_trace)
raise exc_value.with_traceback(exc_trace)


def test_stochasticoptimization():
Expand Down
2 changes: 1 addition & 1 deletion tests/compile/test_function.py
@@ -1,10 +1,10 @@
import six.moves.cPickle as pickle
import os
import shutil
import tempfile

import numpy as np
import pytest
import six.moves.cPickle as pickle

import theano
from theano.compile.io import In
Expand Down
17 changes: 8 additions & 9 deletions tests/compile/test_function_module.py
@@ -1,19 +1,18 @@
import copy
import six.moves.cPickle as pickle
import time

import numpy as np
import pytest
import time
import six.moves.cPickle as pickle

import theano
import theano.tensor as tt
import theano.gpuarray

import theano.tensor as tt
from theano import config, gof
from theano.compat import exc_message
from theano.compile import UnusedInputError, function
from theano.compile.io import In, Out
from theano.compile import function
from theano.compile import UnusedInputError
from theano.gof import MissingInputError
from theano.compat import exc_message


def PatternOptimizer(p1, p2, ign=True):
Expand Down Expand Up @@ -252,7 +251,7 @@ def test_weird_names(self):
def t():
f = function(
[
In(a, name=set(["adsf", ()]), value=1.0),
In(a, name={"adsf", ()}, value=1.0),
In(x, name=(), value=2.0),
In(s, name=tt.scalar(), value=3.0),
],
Expand Down Expand Up @@ -1092,7 +1091,7 @@ def test_pickle_class_with_functions(self):
assert blah.f1[blah.s] != blah2.f1[blah2.s]


class SomethingToPickle(object):
class SomethingToPickle:
def __init__(self):
a = tt.scalar() # the a is for 'anonymous' (un-named).
x, s = tt.scalars("xs")
Expand Down
50 changes: 27 additions & 23 deletions tests/compile/test_misc.py
@@ -1,23 +1,28 @@
import numpy as np

from theano import tensor
from theano.compile.pfunc import pfunc
from theano.compile.sharedvalue import shared
from theano import tensor
from theano.tensor.nnet import sigmoid


class NNet(object):
class NNet:
def __init__(
self,
input=tensor.dvector("input"),
target=tensor.dvector("target"),
input=None,
target=None,
n_input=1,
n_hidden=1,
n_output=1,
lr=1e-3,
**kw,
):
super(NNet, self).__init__(**kw)
super().__init__(**kw)

if input is None:
input = tensor.dvector("input")
if target is None:
target = tensor.dvector("target")

self.input = input
self.target = target
Expand Down Expand Up @@ -46,21 +51,20 @@ def __init__(
self.output_from_hidden = pfunc([self.hidden], self.output)


class TestNnet:
def test_nnet(self):
rng = np.random.RandomState(1827)
data = rng.rand(10, 4)
nnet = NNet(n_input=3, n_hidden=10)
for epoch in range(3):
mean_cost = 0
for x in data:
input = x[0:3]
target = x[3:]
output, cost = nnet.sgd_step(input, target)
mean_cost += cost
mean_cost /= float(len(data))
# print 'Mean cost at epoch %s: %s' % (epoch, mean_cost)
assert abs(mean_cost - 0.20588975452) < 1e-6
# Just call functions to make sure they do not crash.
nnet.compute_output(input)
nnet.output_from_hidden(np.ones(10))
def test_nnet():
rng = np.random.RandomState(1827)
data = rng.rand(10, 4)
nnet = NNet(n_input=3, n_hidden=10)
for epoch in range(3):
mean_cost = 0
for x in data:
input = x[0:3]
target = x[3:]
output, cost = nnet.sgd_step(input, target)
mean_cost += cost
mean_cost /= float(len(data))
# print 'Mean cost at epoch %s: %s' % (epoch, mean_cost)
assert abs(mean_cost - 0.20588975452) < 1e-6
# Just call functions to make sure they do not crash.
nnet.compute_output(input)
nnet.output_from_hidden(np.ones(10))
3 changes: 1 addition & 2 deletions tests/compile/test_mode.py
Expand Up @@ -2,8 +2,7 @@

import theano
import theano.tensor as tt

from theano.compile.mode import Mode, AddFeatureOptimizer
from theano.compile.mode import AddFeatureOptimizer, Mode
from theano.gof.toolbox import NoOutputFromInplace


Expand Down

0 comments on commit 8e8eda2

Please sign in to comment.