Skip to content

Commit

Permalink
general: update mypy rules and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
karlicoss committed Feb 1, 2025
1 parent f0cc3db commit df50277
Show file tree
Hide file tree
Showing 20 changed files with 35 additions and 33 deletions.
9 changes: 6 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ pretty = True
show_error_context = True
show_column_numbers = True
show_error_end = True
warn_redundant_casts = True
warn_unused_ignores = True

check_untyped_defs = True

# see https://mypy.readthedocs.io/en/stable/error_code_list2.html
warn_redundant_casts = True
strict_equality = True
enable_error_code = possibly-undefined
warn_unused_ignores = True
enable_error_code = deprecated,redundant-expr,possibly-undefined,truthy-bool,truthy-iterable,ignore-without-code,unused-awaitable

# todo ok, maybe it wasn't such a good idea..
# mainly because then tox picks it up and running against the user config, not the repository config
Expand Down
2 changes: 1 addition & 1 deletion src/my/arbtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def entries() -> Iterable[Entry]:

from subprocess import PIPE, Popen

import ijson.backends.yajl2_cffi as ijson # type: ignore
import ijson.backends.yajl2_cffi as ijson # type: ignore[import-untyped]
for cmd in cmds:
with Popen(cmd, stdout=PIPE) as p:
out = p.stdout; assert out is not None
Expand Down
2 changes: 1 addition & 1 deletion src/my/calendar/holidays.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@lru_cache(1)
def _calendar():
from workalendar.registry import registry # type: ignore
from workalendar.registry import registry # type: ignore[import-not-found]

# todo switch to using time.tz.main once _get_tz stabilizes?
from ..time.tz import via_location as LTZ
Expand Down
3 changes: 1 addition & 2 deletions src/my/core/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ def modules_check(*, verbose: bool, list_all: bool, quick: bool, for_modules: li

try:
kwargs = {}
# todo hmm why wouldn't they be callable??
if callable(stats) and 'quick' in inspect.signature(stats).parameters:
if 'quick' in inspect.signature(stats).parameters:
kwargs['quick'] = quick
with quick_context:
res = stats(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion src/my/core/_deprecated/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def connect_readonly(db: PathIsh):
import dataset # type: ignore
import dataset # type: ignore[import-not-found]

# see https://github.com/pudo/dataset/issues/136#issuecomment-128693122
# todo not sure if mode=ro has any benefit, but it doesn't work on read-only filesystems
Expand Down
3 changes: 2 additions & 1 deletion src/my/core/denylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ def _deny_cli_remember(
items: Iterator[T],
mem: dict[str, T],
) -> Iterator[str]:
keyf = self._deny_cli_key_func or _default_key_func
# user can override _deny_cli_key_func, so it's not always None, hence the ignore
keyf = self._deny_cli_key_func or _default_key_func # type: ignore[redundant-expr]
# i.e., convert each item to a string, and map str -> item
for item in items:
key = keyf(item)
Expand Down
2 changes: 0 additions & 2 deletions src/my/core/discovery_pure.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ def _extract_requirements(a: ast.Module) -> Requires:
for c in elts:
if isinstance(c, ast.Constant):
deps.append(c.value)
elif isinstance(c, ast.Str):
deps.append(c.s)
else:
raise RuntimeError(f"Expecting string constants only in {REQUIRES} declaration")
return tuple(deps)
Expand Down
2 changes: 1 addition & 1 deletion src/my/core/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def fill(it: Iterable[Any], *, measurement: str, reset: bool = RESET_DEFAULT, dt

db = config.db

from influxdb import InfluxDBClient # type: ignore
from influxdb import InfluxDBClient # type: ignore[import-not-found]

client = InfluxDBClient()
# todo maybe create if not exists?
Expand Down
2 changes: 1 addition & 1 deletion src/my/core/mime.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@functools.lru_cache(1)
def _magic():
import magic # type: ignore
import magic # type: ignore[import-not-found]

# TODO also has uncompess=True? could be useful
return magic.Magic(mime=True)
Expand Down
2 changes: 1 addition & 1 deletion src/my/core/utils/itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def make_dict(
uniques = ensure_unique(with_keys, key=lambda p: p[0])
res: dict[K, V] = {}
for k, i in uniques:
res[k] = i if value is None else value(i)
res[k] = i if value is None else value(i) # type: ignore[redundant-expr]
return res


Expand Down
2 changes: 1 addition & 1 deletion src/my/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def external_module(self):

return import_dir(rpath)

import my.config.repos.external as m # type: ignore
import my.config.repos.external as m # type: ignore[import-not-found]

return m

Expand Down
2 changes: 1 addition & 1 deletion src/my/emfit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class emfit:
# TODO remove/deprecate it? I think used by timeline
def get_datas() -> list[Emfit]:
# todo ugh. run lint properly
return sorted(datas(), key=lambda e: e.start) # type: ignore
return sorted(datas(), key=lambda e: e.start) # type: ignore[arg-type]


# TODO move away old entries if there is a diff??
2 changes: 1 addition & 1 deletion src/my/jawbone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def plot_one(sleep: SleepEntry, fig, axes, xlims=None, *, showtext=True):
print(f"{sleep.xid} span: {span}")

# pip install imageio
from imageio import imread # type: ignore
from imageio import imread # type: ignore[import-not-found]

img = imread(sleep.graph)
# all of them are 300x300 images apparently
Expand Down
8 changes: 4 additions & 4 deletions src/my/jawbone/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from pathlib import Path
from typing import Any, NamedTuple

import matplotlib.pylab as pylab # type: ignore
import matplotlib.pylab as pylab # type: ignore[import-not-found]

# sleep = []
# with open('2017.csv', 'r') as fo:
# reader = DictReader(fo)
# for line in islice(reader, 0, 10):
# sleep
# print(line)
import matplotlib.pyplot as plt # type: ignore
import matplotlib.pyplot as plt # type: ignore[import-not-found]
from numpy import genfromtxt

pylab.rcParams['figure.figsize'] = (32.0, 24.0)
Expand Down Expand Up @@ -93,7 +93,7 @@ def iter_useful(data_file: str):
p / "2017.csv",
]

from kython import concat, parse_date # type: ignore
from kython import concat, parse_date # type: ignore[import-not-found]

useful = concat(*(list(iter_useful(str(f))) for f in files))

Expand All @@ -107,7 +107,7 @@ def iter_useful(data_file: str):
# TODO filter outliers?

# TODO don't need this anymore? it's gonna be in dashboards package
from kython.plotting import plot_timestamped # type: ignore
from kython.plotting import plot_timestamped # type: ignore[import-not-found]

for attr, lims, mavg, fig in [
('light', (0, 400), 5, None),
Expand Down
7 changes: 3 additions & 4 deletions src/my/location/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
from subprocess import PIPE, Popen
from typing import IO, NamedTuple, Optional

# pip3 install geopy
import geopy # type: ignore
import geopy # type: ignore[import-not-found]

from my.core import Stats, make_logger, stat, warnings
from my.core.cachew import cache_dir, mcachew
Expand Down Expand Up @@ -50,10 +49,10 @@ def _iter_via_ijson(fo) -> Iterable[TsLatLon]:
# todo extract to common?
try:
# pip3 install ijson cffi
import ijson.backends.yajl2_cffi as ijson # type: ignore
import ijson.backends.yajl2_cffi as ijson # type: ignore[import-untyped]
except:
warnings.medium("Falling back to default ijson because 'cffi' backend isn't found. It's up to 2x faster, you might want to check it out")
import ijson # type: ignore
import ijson # type: ignore[import-untyped]

for d in ijson.items(fo, 'locations.item'):
yield (
Expand Down
2 changes: 1 addition & 1 deletion src/my/photos/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pathlib import Path
from typing import NamedTuple, Optional

from geopy.geocoders import Nominatim # type: ignore
from geopy.geocoders import Nominatim # type: ignore[import-not-found]

from my.core import LazyLogger
from my.core.cachew import cache_dir, mcachew
Expand Down
10 changes: 6 additions & 4 deletions src/my/rtm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
[[https://rememberthemilk.com][Remember The Milk]] tasks and notes
"""
from __future__ import annotations

REQUIRES = [
'icalendar',
Expand All @@ -11,8 +12,9 @@
from datetime import datetime
from functools import cached_property

import icalendar # type: ignore
from icalendar.cal import Todo # type: ignore
# no mypy annotations... https://github.com/collective/icalendar/issues/395
import icalendar # type: ignore[import-not-found]
from icalendar.cal import Todo # type: ignore[import-not-found]
from more_itertools import bucket

from my.core import get_files, make_logger
Expand Down Expand Up @@ -53,9 +55,9 @@ def uid(self) -> str:
def title(self) -> str:
return str(self.todo['SUMMARY'])

def get_status(self) -> str:
def get_status(self) -> str | None:
if 'STATUS' not in self.todo:
return None # type: ignore
return None
# TODO 'COMPLETED'?
return str(self.todo['STATUS'])

Expand Down
2 changes: 1 addition & 1 deletion src/my/tests/tz.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ def test_policies() -> None:
assert getzone(tz_main.localize(aware, policy='convert')) == 'Europe/Rome'

with pytest.raises(RuntimeError):
assert tz_main.localize(aware, policy='throw')
tz_main.localize(aware, policy='throw')
2 changes: 1 addition & 1 deletion src/my/time/tz/via_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _timezone_finder(*, fast: bool) -> Any:
# less precise, but faster
from timezonefinder import TimezoneFinderL as Finder
else:
from timezonefinder import TimezoneFinder as Finder # type: ignore
from timezonefinder import TimezoneFinder as Finder # type: ignore[assignment]
return Finder(in_memory=True)


Expand Down
2 changes: 1 addition & 1 deletion tests_misc/takeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def test_location_perf() -> None:
# 2.80 s for 10 iterations and 10K points
# TODO try switching to jq and see how it goes? not sure..
print(ilen(islice(LT.iter_locations(), 0, 10000))) # type: ignore
print(ilen(islice(LT.iter_locations(), 0, 10000))) # type: ignore[attr-defined]


# in theory should support any HTML takeout file?
Expand Down

0 comments on commit df50277

Please sign in to comment.