Skip to content

Commit

Permalink
style: fix (or ignore) style warnings surfaced by flake8-bugbear
Browse files Browse the repository at this point in the history
  • Loading branch information
dairiki authored and yagebu committed Aug 29, 2023
1 parent ad6dd3b commit 795a55c
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lektor/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ def checksum(self):
break
h.update(chunk)
checksum = h.hexdigest()
except (OSError, IOError):
except OSError:
checksum = "0" * 40
self._checksum = checksum
return checksum
Expand Down
2 changes: 1 addition & 1 deletion lektor/databags.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def load_databag(filename):
return decode_flat_data(IniFile(filename).items(), dict_cls=OrderedDict)
else:
return None
except (OSError, IOError) as e:
except OSError as e:
if e.errno != errno.ENOENT:
raise
return None
Expand Down
11 changes: 5 additions & 6 deletions lektor/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -1469,12 +1469,11 @@ def get_implied_datamodel(
"""Looks up a datamodel based on the information about the parent
of a model.
"""
dm_name = datamodel
model = datamodel

# Only look for a datamodel if there was not defined.
if dm_name is None:
if model is None:
parent = posixpath.dirname(path)
dm_name = None

# If we hit the root, and there is no model defined we need
# to make sure we do not recurse onto ourselves.
Expand All @@ -1484,11 +1483,11 @@ def get_implied_datamodel(
parent_obj = pad.get(parent)
if parent_obj is not None:
if is_attachment:
dm_name = parent_obj.datamodel.attachment_config.model
model = parent_obj.datamodel.attachment_config.model
else:
dm_name = parent_obj.datamodel.child_config.model
model = parent_obj.datamodel.child_config.model

for dm_name in _iter_datamodel_choices(dm_name, path, is_attachment):
for dm_name in _iter_datamodel_choices(model, path, is_attachment):
# If that datamodel exists, let's roll with it.
datamodel = self.datamodels.get(dm_name)
if datamodel is not None:
Expand Down
6 changes: 3 additions & 3 deletions lektor/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from collections.abc import Mapping
from collections.abc import MutableMapping
from collections.abc import ValuesView
from contextlib import suppress
from functools import wraps
from itertools import chain

Expand Down Expand Up @@ -116,6 +117,7 @@ def wrapper(self, *args, **kwargs):
f"EditorSession.{name} has been deprecated as of Lektor 3.3.2. "
f"Please use EditorSession.data.{newname} instead.",
DeprecationWarning,
stacklevel=2,
)
return wrapped(self, *args, **kwargs)

Expand Down Expand Up @@ -305,10 +307,8 @@ def _page_delete_impl(self):
directory = os.path.dirname(self.fs_path)

if self._recursive_delete:
try:
with suppress(OSError):
shutil.rmtree(directory)
except (OSError, IOError):
pass
return
if self._master_delete:
raise BadDelete(
Expand Down
6 changes: 5 additions & 1 deletion lektor/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,15 @@ def __init__(self, project, load_plugins=True, extra_flags=None):

from lektor.db import F, get_alts # pylint: disable=import-outside-toplevel

def latlongformat(latlong, secs=True):
lat, lon = latlong
return format_lat_long(lat=lat, long=lon, secs=secs)

self.jinja_env.filters.update(
tojson=tojson_filter,
latformat=lambda x, secs=True: format_lat_long(lat=x, secs=secs),
longformat=lambda x, secs=True: format_lat_long(long=x, secs=secs),
latlongformat=lambda x, secs=True: format_lat_long(secs=secs, *x),
latlongformat=latlongformat,
url=_prevent_inlining(url_to),
asseturl=_prevent_inlining(get_asset_url),
markdown=_markdown_filter,
Expand Down
2 changes: 1 addition & 1 deletion lektor/filecontents.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def mimetype(self):
def bytes(self):
try:
return os.stat(self.filename).st_size
except (OSError, IOError):
except OSError:
return 0

def as_data_url(self, mediatype=None):
Expand Down
1 change: 1 addition & 0 deletions lektor/pluginsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,6 @@ def emit(self, event, **kwargs):
"not break if new parameters are passed to it by newer "
"versions of Lektor.",
DeprecationWarning,
stacklevel=2,
)
return rv
2 changes: 1 addition & 1 deletion lektor/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def consolidate_listing(self, con, current_artifacts):
for artifact_name in current_artifacts.keys():
known_folders.add(posixpath.dirname(artifact_name))

for artifact_name, checksum in server_artifacts.items():
for artifact_name, _checksum in server_artifacts.items():
if artifact_name not in current_artifacts:
con.log_buffer.append("000 Deleting %s" % artifact_name)
con.delete_file(artifact_name)
Expand Down
3 changes: 2 additions & 1 deletion lektor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ def __new__(cls, value: str):
"Use the Url.from_string classmethod instead."
),
version="3.4.0",
)
),
stacklevel=2,
)
return cls.from_string(value)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_buildfailures.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ def test_failure_controller_fs_exceptions(failure_controller):
failure_controller.clear_failure("broken")
with pytest.raises(OSError):
try:
assert False
raise RuntimeError("test exception")
except Exception:
failure_controller.store_failure("broken", sys.exc_info())
12 changes: 8 additions & 4 deletions tests/test_pluginsystem.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Unit tests for lektor.pluginsystem.
"""
from __future__ import annotations

import inspect
import sys
from importlib.abc import Loader
Expand Down Expand Up @@ -95,7 +97,7 @@ def create_module(self, spec):
return None

def exec_module(self, module):
setattr(module, "DummyPlugin", DummyPlugin)
module.DummyPlugin = DummyPlugin


class DummyPluginFinder(metadata.DistributionFinder):
Expand All @@ -108,7 +110,9 @@ def find_spec(self, fullname, path, target=None):
return ModuleSpec(fullname, DummyPluginLoader())
return None

def find_distributions(self, context=metadata.DistributionFinder.Context()):
def find_distributions(
self, context: metadata.DistributionFinder.Context | None = None
):
return [self.distribution]


Expand Down Expand Up @@ -182,7 +186,7 @@ def test_env_went_away(self):
plugin = DummyPlugin(env, "dummy-plugin")
del env
with pytest.raises(RuntimeError, match=r"Environment went away"):
getattr(plugin, "env")
plugin.env # pylint: disable=pointless-statement

def test_version(self, dummy_plugin, dummy_plugin_distribution):
assert dummy_plugin.version == dummy_plugin_distribution.version
Expand Down Expand Up @@ -308,7 +312,7 @@ def test_env_went_away(self):
plugin_controller = PluginController(env)
del env
with pytest.raises(RuntimeError, match=r"Environment went away"):
getattr(plugin_controller, "env")
plugin_controller.env # pylint: disable=pointless-statement

def test_instantiate_plugin(self, plugin_controller, env):
plugin_controller.instanciate_plugin("plugin-id", DummyPlugin)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def test_Command_triggers_no_warnings():
gc.collect()

if client_is_alive():
warnings.warn(
warnings.warn( # noqa: B028
"Unable to trigger garbage collection of Command instance, "
"so unable to check for warnings issued during finalization."
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def macos_pause_for_calm(self) -> None:
pass
if not change_seen:
break
warnings.warn(f"macOS settle loop {n}: {change_seen}")
warnings.warn(f"macOS settle loop {n}: {change_seen}") # noqa: B028


@pytest.fixture
Expand Down

0 comments on commit 795a55c

Please sign in to comment.