Skip to content

Commit

Permalink
Merge pull request #930 from hramezani/pyupgrade
Browse files Browse the repository at this point in the history
Add pyupgrade to pre-commit and update syntax to Python3.7+
  • Loading branch information
nolar committed Nov 1, 2022
2 parents a251e84 + 668f315 commit 04d8f02
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ repos:
name: isort-examples
args: [--settings=examples]
files: '^examples/'

- repo: https://github.com/asottile/pyupgrade
rev: v2.34.0
hooks:
- id: pyupgrade
args: [--py37-plus]
2 changes: 1 addition & 1 deletion _importlinter_conditional.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _check_secured_import(self, mod: str, lno: int) -> bool:
# Some hard-coded heuristics because importlib fails on circular imports.
# TODO: switch to: importlib.util.find_spec(mod)?.origin
path = os.path.join(os.path.dirname(__file__), mod.replace('.', '/')) + '.py'
with open(path, 'rt', encoding='utf-8') as f:
with open(path, encoding='utf-8') as f:
text = f.read()
xtree = astpath.file_contents_to_xml_ast(text)

Expand Down
4 changes: 2 additions & 2 deletions examples/13-hooks/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ async def login_fn(**kwargs):

@kopf.on.probe()
async def tasks_count(**kwargs):
return sum([len(flags) for flags in STOPPERS.values()])
return sum(len(flags) for flags in STOPPERS.values())


@kopf.on.probe()
async def monitored_objects(**kwargs):
return {namespace: sorted([name for name in STOPPERS[namespace]]) for namespace in STOPPERS}
return {namespace: sorted(name for name in STOPPERS[namespace]) for namespace in STOPPERS}


@kopf.on.event('pods')
Expand Down
2 changes: 1 addition & 1 deletion kopf/_cogs/helpers/hostnames.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_descriptive_hostname() -> str:
"""
try:
hostname, aliases, ipaddrs = socket.gethostbyaddr(socket.gethostname())
except socket.error:
except OSError:
pass
else:
ipv4: Optional[ipaddress.IPv4Address]
Expand Down
6 changes: 3 additions & 3 deletions kopf/_core/intents/piggybacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ def login_with_service_account(**_: Any) -> Optional[credentials.ConnectionInfo]
ca_path = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'

if os.path.exists(token_path):
with open(token_path, 'rt', encoding='utf-8') as f:
with open(token_path, encoding='utf-8') as f:
token = f.read().strip()

namespace: Optional[str] = None
if os.path.exists(ns_path):
with open(ns_path, 'rt', encoding='utf-8') as f:
with open(ns_path, encoding='utf-8') as f:
namespace = f.read().strip()

return credentials.ConnectionInfo(
Expand Down Expand Up @@ -226,7 +226,7 @@ def login_with_kubeconfig(**_: Any) -> Optional[credentials.ConnectionInfo]:
users: Dict[Any, Any] = {}
for path in paths:

with open(path, 'rt', encoding='utf-8') as f:
with open(path, encoding='utf-8') as f:
config = yaml.safe_load(f.read()) or {}

if current_context is None:
Expand Down
3 changes: 1 addition & 2 deletions kopf/_core/reactor/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ def __init__(self) -> None:
self._items = {}

def iter_all_memories(self) -> Iterator[ResourceMemory]:
for memory in self._items.values():
yield memory
yield from self._items.values()

def iter_all_daemon_memories(self) -> Iterator[daemons.DaemonsMemory]:
for memory in self._items.values():
Expand Down
2 changes: 0 additions & 2 deletions tests/cli/test_help.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


def test_help_in_root(invoke, mocker):
result = invoke(['--help'])

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ def timer():
return Timer()


class Timer(object):
class Timer:
"""
A helper context manager to measure the time of the code-blocks.
Also, supports direct comparison with time-deltas and the numbers of seconds.
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
root_dir = os.path.relpath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
examples = sorted(glob.glob(os.path.join(root_dir, 'examples/*/')))
assert examples # if empty, it is just the detection failed
examples = [path for path in examples if not glob.glob((os.path.join(path, 'test*.py')))]
examples = [path for path in examples if not glob.glob(os.path.join(path, 'test*.py'))]


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class E2EParser:
def __init__(self, path: str) -> None:
super().__init__()

with open(path, 'rt', encoding='utf-8') as f:
with open(path, encoding='utf-8') as f:
self.path = path
self.text = f.read()

Expand Down
3 changes: 1 addition & 2 deletions tests/hierarchies/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def __init__(self, objs):
self._objs = objs

def __iter__(self):
for obj in self._objs:
yield obj
yield from self._objs


@pytest.fixture(params=[list, tuple, CustomIterable],
Expand Down
4 changes: 2 additions & 2 deletions tests/reactor/test_queueing.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ async def test_watchevent_batching(settings, resource, processor, timer,
assert processor.call_count == len(uids)
assert processor.call_count == len(vals)
expected_uid_val_pairs = set(zip(uids, vals))
actual_uid_val_pairs = set((
actual_uid_val_pairs = {(
kwargs['raw_event']['object']['metadata']['uid'],
kwargs['raw_event']['object']['spec'])
for args, kwargs in processor.call_args_list)
for args, kwargs in processor.call_args_list}
assert actual_uid_val_pairs == expected_uid_val_pairs


Expand Down
2 changes: 0 additions & 2 deletions tests/test_it.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


def test_importing_works():
import kopf
assert kopf
2 changes: 1 addition & 1 deletion tests/timing/test_throttling.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async def test_renews_on_repeated_failure(sleep):
async with throttled(throttler=throttler, logger=logger, delays=[234]):
raise Exception()

assert throttler.last_used_delay is 234
assert throttler.last_used_delay == 234
assert throttler.source_of_delays is not None
assert throttler.active_until is None
assert sleep.mock_calls == [call(234, wakeup=None)]
Expand Down

0 comments on commit 04d8f02

Please sign in to comment.