Skip to content

refactor: Run isort and black on all python files #1859

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

Merged
merged 5 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ A brief description of the categories of changes:
`pip.parse` extension is now possible, see the
`examples/pip_parse/MODULE.bazel` for how to do it.
See [#1371](https://github.com/bazelbuild/rules_python/issues/1371).
* (refactor) The pre-commit developer workflow should now pass `isort` and `black`
checks (see [#1674](https://github.com/bazelbuild/rules_python/issues/1674)).

### Added

Expand Down
10 changes: 6 additions & 4 deletions examples/build_file_generation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sphinx # noqa
from flask import Flask, jsonify
from random_number_generator import generate_random_number
import sphinx # noqa

app = Flask(__name__)

@app.route('/random-number', methods=['GET'])

@app.route("/random-number", methods=["GET"])
def get_random_number():
return jsonify({'number': generate_random_number.generate_random_number()})
return jsonify({"number": generate_random_number.generate_random_number()})


"""Start the python web server"""
def main():
"""Start the python web server"""
app.run()
2 changes: 1 addition & 1 deletion examples/build_file_generation/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@

from __init__ import main

if __name__ == '__main__':
if __name__ == "__main__":
main()
13 changes: 8 additions & 5 deletions examples/build_file_generation/__test__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@
# limitations under the License.

import unittest

from __init__ import app


class TestServer(unittest.TestCase):
def setUp(self):
self.app = app.test_client()

def test_get_random_number(self):
response = self.app.get('/random-number')
response = self.app.get("/random-number")
self.assertEqual(response.status_code, 200)
self.assertIn('number', response.json)

if __name__ == '__main__':
self.assertIn("number", response.json)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
# limitations under the License.

import unittest

import random_number_generator.generate_random_number as generate_random_number


class TestRandomNumberGenerator(unittest.TestCase):
def test_generate_random_number(self):
number = generate_random_number.generate_random_number()
self.assertGreaterEqual(number, 1)
self.assertLessEqual(number, 10)

if __name__ == '__main__':


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import random

"""Generate a random number"""

def generate_random_number():
"""Generate a random number"""
return random.randint(1, 10)
3 changes: 2 additions & 1 deletion examples/bzlmod/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from lib import main
import sys

from lib import main

if __name__ == "__main__":
print(main([["A", 1], ["B", 2]]))
print(sys.version)
3 changes: 2 additions & 1 deletion examples/bzlmod/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from tabulate import tabulate
import sphinx # noqa
from tabulate import tabulate


def main(table):
return tabulate(table)
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def testCurrentRepository(self):
self.assertEqual(runfiles.Create().CurrentRepository(), "")

def testRunfilesWithRepoMapping(self):
data_path = runfiles.Create().Rlocation("example_bzlmod_build_file_generation/runfiles/data/data.txt")
data_path = runfiles.Create().Rlocation(
"example_bzlmod_build_file_generation/runfiles/data/data.txt"
)
with open(data_path) as f:
self.assertEqual(f.read().strip(), "Hello, example_bzlmod!")

Expand Down
8 changes: 5 additions & 3 deletions examples/multi_python_versions/tests/my_lib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
if not my_lib.websockets_is_for_python_version(
workspace_version
) and not my_lib.websockets_is_for_python_version(bzlmod_version):
print("expected package for Python version is different than returned\n"
f"expected either {workspace_version} or {bzlmod_version}\n"
f"but got {my_lib.websockets.__file__}")
print(
"expected package for Python version is different than returned\n"
f"expected either {workspace_version} or {bzlmod_version}\n"
f"but got {my_lib.websockets.__file__}"
)
sys.exit(1)
5 changes: 1 addition & 4 deletions examples/pip_parse/pip_parse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ class PipInstallTest(unittest.TestCase):
def _remove_leading_dirs(self, paths):
# Removes the first two directories (external/<reponame>)
# to normalize what workspace and bzlmod produce.
return [
'/'.join(v.split('/')[2:])
for v in paths
]
return ["/".join(v.split("/")[2:]) for v in paths]

def test_entry_point(self):
entry_point_path = os.environ.get("YAMLLINT_ENTRY_POINT")
Expand Down
5 changes: 3 additions & 2 deletions examples/py_proto_library/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

from another_proto import message_pb2


class TestCase(unittest.TestCase):
def test_message(self):
got = message_pb2.TestMessage(
index = 5,
index=5,
)
self.assertIsNotNone(got)


if __name__ == "__main__":
sys.exit(unittest.main())
sys.exit(unittest.main())
2 changes: 1 addition & 1 deletion examples/wheel/private/directory_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def main() -> None:

args.output.mkdir(parents=True, exist_ok=True)

for (path, content) in args.files:
for path, content in args.files:
new_file = args.output / path
new_file.parent.mkdir(parents=True, exist_ok=True)
new_file.write_text(content)
Expand Down
4 changes: 2 additions & 2 deletions examples/wheel/wheel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def assertAllEntriesHasReproducibleMetadata(self, zf):
self.assertEqual(
zinfo.external_attr,
(stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO | stat.S_IFREG) << 16,
msg=zinfo.filename
msg=zinfo.filename,
)
self.assertEqual(
zinfo.compress_type, zipfile.ZIP_DEFLATED, msg=zinfo.filename
Expand Down Expand Up @@ -486,7 +486,7 @@ def test_minimal_data_files(self):
"minimal_data_files-0.0.1.data/data/target/path/README.md",
"minimal_data_files-0.0.1.data/scripts/NOTICE",
"minimal_data_files-0.0.1.dist-info/RECORD",
]
],
)


Expand Down
2 changes: 1 addition & 1 deletion gazelle/manifest/copy_to_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def copy_to_source(generated_relative_path: Path, target_relative_path: Path) ->
target_absolute_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(generated_absolute_path, target_absolute_path)

target_absolute_path.chmod(0O664)
target_absolute_path.chmod(0o664)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion gazelle/python/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def parse_main(content):
if token_type != OP or token_val != "==":
continue
token_type, token_val, start, _, _ = next(g)
if token_type != STRING or token_val.strip("\"'") != '__main__':
if token_type != STRING or token_val.strip("\"'") != "__main__":
continue
token_type, token_val, start, _, _ = next(g)
if token_type != OP or token_val != ":":
Expand Down
2 changes: 2 additions & 0 deletions gazelle/python/parse_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import unittest

import parse


class TestParse(unittest.TestCase):
def test_not_has_main(self):
content = "a = 1\nb = 2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import numpy

if __name__ == "__main__":
run()
run()
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
import pandas

if __name__ == "__main__":
run()
run()
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import unittest


class TestMain(unittest.unittest):
pass


if __name__ == "__main__":
unittest.main()
1 change: 0 additions & 1 deletion gazelle/python/testdata/dont_rename_target/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


def baz():
return "baz from baz.py"
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


def foo():
print("foo")
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


def two():
return "two"
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


def find_me():
return "found"
1 change: 1 addition & 0 deletions gazelle/python/testdata/generated_test_entrypoint/foo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


def foo():
return "foo"
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

2 changes: 1 addition & 1 deletion gazelle/python/testdata/naming_convention/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
import __init__
2 changes: 1 addition & 1 deletion gazelle/python/testdata/naming_convention/__test__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
import __init__
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
import __init__
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
import __init__
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
import __init__
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
import __init__
1 change: 1 addition & 0 deletions gazelle/python/testdata/per_file_subdirs/bar/foo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


def func():
pass
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import boto3
import __init__
import boto3

_ = boto3
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.

# For test purposes only.
import __init__
import __init__
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


def function2():
return "function2"
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.


class Class1:
def method1(self):
return "method1"
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import resolved_package

from . import Class1
from .subpackage1.module5 import function5

import resolved_package

def function3():
c1 = Class1()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


def function4():
return "function4"
Loading
Loading