Skip to content

Commit d2ca95d

Browse files
authored
Merge pull request #22 from jelmer/ruff-format
Format with ruff
2 parents 16829b1 + 116ff4d commit d2ca95d

File tree

5 files changed

+79
-69
lines changed

5 files changed

+79
-69
lines changed

.github/workflows/python-package.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ jobs:
2323
run: |
2424
python -m pip install --upgrade pip
2525
python -m pip install testtools
26+
python -m pip install ruff
2627
- name: Test with testtools
2728
run: |
2829
python -m testtools.run extras.tests.test_suite
30+
- name: Lint with ruff
31+
run: |
32+
python -m ruff check .

extras/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import sys
66

77
__all__ = [
8-
'try_import',
9-
'try_imports',
10-
]
8+
"try_import",
9+
"try_imports",
10+
]
1111

1212
# same format as sys.version_info: "A tuple containing the five components of
1313
# the version number: major, minor, micro, releaselevel, and serial. All
@@ -21,7 +21,7 @@
2121
# If the releaselevel is 'final', then the tarball will be major.minor.micro.
2222
# Otherwise it is major.minor.micro~$(revno).
2323

24-
__version__ = (1, 0, 0, 'final', 0)
24+
__version__ = (1, 0, 0, "final", 0)
2525

2626

2727
def try_import(name, alternative=None, error_callback=None):
@@ -37,14 +37,14 @@ def try_import(name, alternative=None, error_callback=None):
3737
:param error_callback: If non-None, a callable that is passed the ImportError
3838
when the module cannot be loaded.
3939
"""
40-
module_segments = name.split('.')
40+
module_segments = name.split(".")
4141
last_error = None
4242
remainder = []
4343
# module_name will be what successfully imports. We cannot walk from the
4444
# __import__ result because in import loops (A imports A.B, which imports
4545
# C, which calls try_import("A.B")) A.B will not yet be set.
4646
while module_segments:
47-
module_name = '.'.join(module_segments)
47+
module_name = ".".join(module_segments)
4848
try:
4949
__import__(module_name)
5050
except ImportError:
@@ -69,6 +69,8 @@ def try_import(name, alternative=None, error_callback=None):
6969

7070

7171
_RAISE_EXCEPTION = object()
72+
73+
7274
def try_imports(module_names, alternative=_RAISE_EXCEPTION, error_callback=None):
7375
"""Attempt to import modules.
7476
@@ -92,6 +94,5 @@ def try_imports(module_names, alternative=_RAISE_EXCEPTION, error_callback=None)
9294
if module:
9395
return module
9496
if alternative is _RAISE_EXCEPTION:
95-
raise ImportError(
96-
"Could not import any of: %s" % ', '.join(module_names))
97+
raise ImportError("Could not import any of: %s" % ", ".join(module_names))
9798
return alternative

extras/tests/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
def test_suite():
99
from extras.tests import (
1010
test_extras,
11-
)
11+
)
12+
1213
modules = [
1314
test_extras,
14-
]
15+
]
1516
loader = TestLoader()
1617
suites = map(loader.loadTestsFromModule, modules)
1718
return TestSuite(suites)

extras/tests/test_extras.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
Equals,
99
Is,
1010
Not,
11-
)
11+
)
1212

1313
from extras import (
1414
try_import,
1515
try_imports,
16-
)
16+
)
1717

18-
def check_error_callback(test, function, arg, expected_error_count,
19-
expect_result):
18+
19+
def check_error_callback(test, function, arg, expected_error_count, expect_result):
2020
"""General test template for error_callback argument.
2121
2222
:param test: Test case instance.
@@ -27,9 +27,11 @@ def check_error_callback(test, function, arg, expected_error_count,
2727
ultimately be returned or not.
2828
"""
2929
cb_calls = []
30+
3031
def cb(e):
3132
test.assertIsInstance(e, ImportError)
3233
cb_calls.append(e)
34+
3335
try:
3436
result = function(arg, error_callback=cb)
3537
except ImportError:
@@ -43,58 +45,60 @@ def cb(e):
4345

4446

4547
class TestTryImport(TestCase):
46-
4748
def test_doesnt_exist(self):
4849
# try_import('thing', foo) returns foo if 'thing' doesn't exist.
4950
marker = object()
50-
result = try_import('doesntexist', marker)
51+
result = try_import("doesntexist", marker)
5152
self.assertThat(result, Is(marker))
5253

5354
def test_None_is_default_alternative(self):
5455
# try_import('thing') returns None if 'thing' doesn't exist.
55-
result = try_import('doesntexist')
56+
result = try_import("doesntexist")
5657
self.assertThat(result, Is(None))
5758

5859
def test_existing_module(self):
5960
# try_import('thing', foo) imports 'thing' and returns it if it's a
6061
# module that exists.
61-
result = try_import('os', object())
62+
result = try_import("os", object())
6263
import os
64+
6365
self.assertThat(result, Is(os))
6466

6567
def test_existing_submodule(self):
6668
# try_import('thing.another', foo) imports 'thing' and returns it if
6769
# it's a module that exists.
68-
result = try_import('os.path', object())
70+
result = try_import("os.path", object())
6971
import os
72+
7073
self.assertThat(result, Is(os.path))
7174

7275
def test_nonexistent_submodule(self):
7376
# try_import('thing.another', foo) imports 'thing' and returns foo if
7477
# 'another' doesn't exist.
7578
marker = object()
76-
result = try_import('os.doesntexist', marker)
79+
result = try_import("os.doesntexist", marker)
7780
self.assertThat(result, Is(marker))
7881

7982
def test_object_from_module(self):
8083
# try_import('thing.object') imports 'thing' and returns
8184
# 'thing.object' if 'thing' is a module and 'object' is not.
82-
result = try_import('os.path.join')
85+
result = try_import("os.path.join")
8386
import os
87+
8488
self.assertThat(result, Is(os.path.join))
8589

8690
def test_error_callback(self):
8791
# the error callback is called on failures.
88-
check_error_callback(self, try_import, 'doesntexist', 1, False)
92+
check_error_callback(self, try_import, "doesntexist", 1, False)
8993

9094
def test_error_callback_missing_module_member(self):
9195
# the error callback is called on failures to find an object
9296
# inside an existing module.
93-
check_error_callback(self, try_import, 'os.nonexistent', 1, False)
97+
check_error_callback(self, try_import, "os.nonexistent", 1, False)
9498

9599
def test_error_callback_not_on_success(self):
96100
# the error callback is not called on success.
97-
check_error_callback(self, try_import, 'os.path', 0, True)
101+
check_error_callback(self, try_import, "os.path", 0, True)
98102

99103
def test_handle_partly_imported_name(self):
100104
# try_import('thing.other') when thing.other is mid-import
@@ -114,62 +118,60 @@ def test_handle_partly_imported_name(self):
114118

115119

116120
class TestTryImports(TestCase):
117-
118121
def test_doesnt_exist(self):
119122
# try_imports('thing', foo) returns foo if 'thing' doesn't exist.
120123
marker = object()
121-
result = try_imports(['doesntexist'], marker)
124+
result = try_imports(["doesntexist"], marker)
122125
self.assertThat(result, Is(marker))
123126

124127
def test_fallback(self):
125-
result = try_imports(['doesntexist', 'os'])
128+
result = try_imports(["doesntexist", "os"])
126129
import os
130+
127131
self.assertThat(result, Is(os))
128132

129133
def test_None_is_default_alternative(self):
130134
# try_imports('thing') returns None if 'thing' doesn't exist.
131-
e = self.assertRaises(
132-
ImportError, try_imports, ['doesntexist', 'noreally'])
135+
e = self.assertRaises(ImportError, try_imports, ["doesntexist", "noreally"])
133136
self.assertThat(
134-
str(e),
135-
Equals("Could not import any of: doesntexist, noreally"))
137+
str(e), Equals("Could not import any of: doesntexist, noreally")
138+
)
136139

137140
def test_existing_module(self):
138141
# try_imports('thing', foo) imports 'thing' and returns it if it's a
139142
# module that exists.
140-
result = try_imports(['os'], object())
143+
result = try_imports(["os"], object())
141144
import os
145+
142146
self.assertThat(result, Is(os))
143147

144148
def test_existing_submodule(self):
145149
# try_imports('thing.another', foo) imports 'thing' and returns it if
146150
# it's a module that exists.
147-
result = try_imports(['os.path'], object())
151+
result = try_imports(["os.path"], object())
148152
import os
153+
149154
self.assertThat(result, Is(os.path))
150155

151156
def test_nonexistent_submodule(self):
152157
# try_imports('thing.another', foo) imports 'thing' and returns foo if
153158
# 'another' doesn't exist.
154159
marker = object()
155-
result = try_imports(['os.doesntexist'], marker)
160+
result = try_imports(["os.doesntexist"], marker)
156161
self.assertThat(result, Is(marker))
157162

158163
def test_fallback_submodule(self):
159-
result = try_imports(['os.doesntexist', 'os.path'])
164+
result = try_imports(["os.doesntexist", "os.path"])
160165
import os
166+
161167
self.assertThat(result, Is(os.path))
162168

163169
def test_error_callback(self):
164170
# One error for every class that doesn't exist.
165-
check_error_callback(self, try_imports,
166-
['os.doesntexist', 'os.notthiseither'],
167-
2, False)
168-
check_error_callback(self, try_imports,
169-
['os.doesntexist', 'os.notthiseither', 'os'],
170-
2, True)
171-
check_error_callback(self, try_imports,
172-
['os.path'],
173-
0, True)
174-
175-
171+
check_error_callback(
172+
self, try_imports, ["os.doesntexist", "os.notthiseither"], 2, False
173+
)
174+
check_error_callback(
175+
self, try_imports, ["os.doesntexist", "os.notthiseither", "os"], 2, True
176+
)
177+
check_error_callback(self, try_imports, ["os.path"], 0, True)

setup.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,38 @@
55
import os.path
66

77
import extras
8-
testtools_cmd = extras.try_import('testtools.TestCommand')
8+
9+
testtools_cmd = extras.try_import("testtools.TestCommand")
910

1011

1112
def get_version():
1213
"""Return the version of extras that we are building."""
13-
version = '.'.join(
14-
str(component) for component in extras.__version__[0:3])
14+
version = ".".join(str(component) for component in extras.__version__[0:3])
1515
return version
1616

1717

1818
def get_long_description():
19-
readme_path = os.path.join(
20-
os.path.dirname(__file__), 'README.rst')
19+
readme_path = os.path.join(os.path.dirname(__file__), "README.rst")
2120
return open(readme_path).read()
2221

2322

2423
cmdclass = {}
2524

2625
if testtools_cmd is not None:
27-
cmdclass['test'] = testtools_cmd
28-
29-
30-
setup(name='extras',
31-
author='Testing cabal',
32-
author_email='testtools-dev@lists.launchpad.net',
33-
url='https://github.com/testing-cabal/extras',
34-
description=('Useful extra bits for Python - things that should be '
35-
'in the standard library'),
36-
long_description=get_long_description(),
37-
version=get_version(),
38-
classifiers=[
26+
cmdclass["test"] = testtools_cmd
27+
28+
29+
setup(
30+
name="extras",
31+
author="Testing cabal",
32+
author_email="testtools-dev@lists.launchpad.net",
33+
url="https://github.com/testing-cabal/extras",
34+
description=(
35+
"Useful extra bits for Python - things that should be in the standard library"
36+
),
37+
long_description=get_long_description(),
38+
version=get_version(),
39+
classifiers=[
3940
"Intended Audience :: Developers",
4041
"License :: OSI Approved :: MIT License",
4142
"Programming Language :: Python",
@@ -48,9 +49,10 @@ def get_long_description():
4849
"Programming Language :: Python :: 3.11",
4950
"Programming Language :: Python :: Implementation :: CPython",
5051
"Programming Language :: Python :: Implementation :: PyPy",
51-
],
52-
packages=[
53-
'extras',
54-
'extras.tests',
55-
],
56-
cmdclass=cmdclass)
52+
],
53+
packages=[
54+
"extras",
55+
"extras.tests",
56+
],
57+
cmdclass=cmdclass,
58+
)

0 commit comments

Comments
 (0)