Skip to content

Commit

Permalink
fix: fixed warnings/nits reported by ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Mar 11, 2023
1 parent b6362bb commit 085d823
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 127 deletions.
7 changes: 3 additions & 4 deletions aw_core/decorators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import warnings
import functools
import logging
import time
import warnings


def deprecated(f): # pragma: no cover
Expand All @@ -22,9 +22,8 @@ def g(*args, **kwargs):
if not warned_for:
warnings.simplefilter("always", DeprecationWarning) # turn off filter
warnings.warn(
"Call to deprecated function {}, this warning will only show once per function.".format(
f.__name__
),
"Call to deprecated function {}, "
"this warning will only show once per function.".format(f.__name__),
category=DeprecationWarning,
stacklevel=2,
)
Expand Down
11 changes: 8 additions & 3 deletions aw_core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from . import dirs
from .decorators import deprecated

# NOTE: Will be removed in a future version since it's not compatible with running a multi-service process
# NOTE: Will be removed in a future version since it's not compatible
# with running a multi-service process.
# TODO: prefix with `_`
log_file_path = None

Expand Down Expand Up @@ -50,15 +51,19 @@ def setup_logging(

def excepthook(type_, value, traceback):
root_logger.exception("Unhandled exception", exc_info=(type_, value, traceback))
# call the default excepthook if log_stderr isn't true (otherwise it'll just get duplicated)
# call the default excepthook if log_stderr isn't true
# (otherwise it'll just get duplicated)
if not log_stderr:
sys.__excepthook__(type_, value, traceback)

sys.excepthook = excepthook


def _get_latest_log_files(name, testing=False) -> List[str]: # pragma: no cover
"""Returns a list with the paths of all available logfiles for `name` sorted by latest first."""
"""
Returns a list with the paths of all available logfiles for `name`,
sorted by latest first.
"""
log_dir = dirs.get_log_dir(name)
files = filter(lambda filename: name in filename, os.listdir(log_dir))
files = filter(
Expand Down
13 changes: 10 additions & 3 deletions aw_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import numbers
import typing
from datetime import datetime, timedelta, timezone
from typing import Any, List, Dict, Union, Optional
from typing import (
Any,
Dict,
Optional,
Union,
)

import iso8601

Expand Down Expand Up @@ -50,12 +55,14 @@ def __init__(
self.id = id
if timestamp is None:
logger.warning(
"Event initializer did not receive a timestamp argument, using now as timestamp"
"Event initializer did not receive a timestamp argument, "
"using now as timestamp"
)
# FIXME: The typing.cast here was required for mypy to shut up, weird...
self.timestamp = datetime.now(typing.cast(timezone, timezone.utc))
else:
# The conversion needs to be explicit here for mypy to pick it up (lacks support for properties)
# The conversion needs to be explicit here for mypy to pick it up
# (lacks support for properties)
self.timestamp = _timestamp_parse(timestamp)
self.duration = duration # type: ignore
self.data = data
Expand Down
24 changes: 15 additions & 9 deletions aw_datastore/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
from typing import Dict, Callable, Any
import platform as _platform

from .migration import check_for_migration
from typing import Callable, Dict

from . import storages
from .datastore import Datastore
from .migration import check_for_migration

# The Callable[[Any], ...] here should be Callable[..., ...] but Python 3.5.2 doesn't
# like ellipsis. See here: https://github.com/python/typing/issues/259
def get_storage_methods() -> Dict[str, Callable[[Any], storages.AbstractStorage]]:
from .storages import MemoryStorage, MongoDBStorage, PeeweeStorage, SqliteStorage

methods: Dict[str, Callable[[Any], storages.AbstractStorage]] = {
def get_storage_methods() -> Dict[str, Callable[..., storages.AbstractStorage]]:
from .storages import (
MemoryStorage,
MongoDBStorage,
PeeweeStorage,
SqliteStorage,
)

methods: Dict[str, Callable[..., storages.AbstractStorage]] = {
PeeweeStorage.sid: PeeweeStorage,
MemoryStorage.sid: MemoryStorage,
SqliteStorage.sid: SqliteStorage,
Expand All @@ -20,10 +23,13 @@ def get_storage_methods() -> Dict[str, Callable[[Any], storages.AbstractStorage]
# MongoDB is not supported on Windows or macOS
if _platform.system() == "Linux": # pragma: no branch
try:
import pymongo
import pymongo # noqa: F401

methods[MongoDBStorage.sid] = MongoDBStorage
except ImportError: # pragma: no cover
pass

return methods


__all__ = ["Datastore", "get_storage_methods", "check_for_migration"]
36 changes: 22 additions & 14 deletions aw_datastore/datastore.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import logging
from datetime import datetime, timezone, timedelta
from typing import Dict, List, Union, Callable, Optional
from datetime import datetime, timedelta, timezone
from typing import (
Callable,
Dict,
List,
Optional,
Union,
)

from aw_core.models import Event

Expand Down Expand Up @@ -122,14 +128,16 @@ def insert(self, events: Union[Event, List[Event]]) -> Optional[Event]:
If a single event is inserted, return the event with its id assigned.
If several events are inserted, returns None. (This is due to there being no efficient way of getting ids out when doing bulk inserts with some datastores such as peewee/SQLite)
"""

# NOTE: Should we keep the timestamp checking?
warn_older_event = False

# Get last event for timestamp check after insert
"""
last_event_list = self.get(1)
last_event = None
if last_event_list:
last_event = last_event_list[0]
"""
if warn_older_event:
last_event_list = self.get(1)
last_event = None
if last_event_list:
last_event = last_event_list[0]

now = datetime.now(tz=timezone.utc)

Expand Down Expand Up @@ -163,13 +171,13 @@ def insert(self, events: Union[Event, List[Event]]) -> Optional[Event]:
raise TypeError

# Warn if timestamp is older than last event
"""
if last_event and oldest_event:
if warn_older_event and last_event and oldest_event:
if oldest_event.timestamp < last_event.timestamp: # pragma: no cover
self.logger.warning("Inserting event that has a older timestamp than previous event!" +
"\nPrevious:" + str(last_event) +
"\nInserted:" + str(oldest_event))
"""
self.logger.warning(
f"""Inserting event that has a older timestamp than previous event!
Previous: {last_event}
Inserted: {oldest_event}"""
)

return inserted

Expand Down
6 changes: 3 additions & 3 deletions aw_datastore/migration.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Optional, List
import os
import re
import logging
import os
from typing import List, Optional

from aw_core.dirs import get_data_dir

from .storages import AbstractStorage

logger = logging.getLogger(__name__)
Expand Down
9 changes: 8 additions & 1 deletion aw_datastore/storages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
logger: _logging.Logger = _logging.getLogger(__name__)

from .abstract import AbstractStorage

from .memory import MemoryStorage
from .mongodb import MongoDBStorage
from .peewee import PeeweeStorage
from .sqlite import SqliteStorage

__all__ = [
"AbstractStorage",
"MemoryStorage",
"MongoDBStorage",
"PeeweeStorage",
"SqliteStorage",
]
5 changes: 2 additions & 3 deletions aw_datastore/storages/abstract.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from typing import List, Dict, Optional
from abc import ABCMeta, abstractmethod
from datetime import datetime
from abc import ABCMeta, abstractmethod, abstractproperty
from typing import Dict, List, Optional

from aw_core.models import Event

Expand Down
3 changes: 0 additions & 3 deletions aw_datastore/storages/mongodb.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import logging
from typing import List, Dict, Optional
from datetime import datetime, timezone

import iso8601

# will be unavailable if pymongo is
try:
from bson.objectid import ObjectId
Expand Down
5 changes: 2 additions & 3 deletions aw_query/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from . import exceptions
from . import query2

from .query2 import query

__all__ = ["query"]
1 change: 0 additions & 1 deletion aw_query/functions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
import iso8601
from typing import Optional, Callable, Dict, Any, List
from inspect import signature
Expand Down
8 changes: 4 additions & 4 deletions aw_query/query2.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,19 +290,19 @@ def interpret(self, datastore: Datastore, namespace: dict):
@staticmethod
def parse(string: str, namespace: dict) -> QToken:
entries_str = string[1:-1]
l: List[QToken] = []
ls: List[QToken] = []
while len(entries_str) > 0:
entries_str = entries_str.strip()
if len(l) > 0 and entries_str[0] == ",":
if len(ls) > 0 and entries_str[0] == ",":
entries_str = entries_str[1:]
# parse
(val_t, val_str), entries_str = _parse_token(entries_str, namespace)
if not val_t:
raise QueryParseException("List expected a value, got nothing")
val = val_t.parse(val_str, namespace)
# set
l.append(val)
return QList(l)
ls.append(val)
return QList(ls)

@staticmethod
def check(string: str):
Expand Down
2 changes: 1 addition & 1 deletion aw_transform/chunk_events_by_key.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from typing import List, Dict
from datetime import timedelta
from typing import List

from aw_core.models import Event

Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import context
from . import context # noqa: F401
19 changes: 12 additions & 7 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import shutil
from configparser import ConfigParser
from pathlib import Path

import pytest
import deprecation

import pytest
from aw_core import dirs
from aw_core.config import load_config, save_config, load_config_toml, save_config_toml
from aw_core.config import (
load_config,
load_config_toml,
save_config,
save_config_toml,
)

appname = "aw-core-test"
section = "section"
Expand Down Expand Up @@ -34,8 +39,8 @@ def clean_config():

def test_create():
appname = "aw-core-test"
section = "section"
config_dir = dirs.get_config_dir(appname)
assert Path(config_dir).exists()


def test_config_defaults():
Expand All @@ -61,9 +66,9 @@ def test_config_no_defaults():

def test_config_override():
# Create a minimal config file with one overridden value
config = """[section]
config_str = """[section]
somevalue = 1000.1"""
save_config_toml(appname, config)
save_config_toml(appname, config_str)

# Open non-default config file and verify that values are correct
config = load_config_toml(appname, default_config_str)
Expand All @@ -74,7 +79,7 @@ def test_config_override():
def test_config_ini():
# Create default config
default_config = ConfigParser()
default_config[section] = {"somestring": "Hello World!", "somevalue": 12.3}
default_config[section] = {"somestring": "Hello World!", "somevalue": 12.3} # type: ignore

# Load non-existing config (will create a default config file)
config = load_config(appname, default_config)
Expand Down
8 changes: 2 additions & 6 deletions tests/test_datastore.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import logging
import random
import iso8601
from datetime import datetime, timedelta, timezone
import iso8601

import iso8601
import pytest

from . import context

from aw_core.models import Event
from aw_datastore import get_storage_methods

from . import context # noqa: F401
from .utils import param_datastore_objects, param_testing_buckets_cm


logging.basicConfig(level=logging.DEBUG)

# Useful when you just want some placeholder time in your events, saves typing
Expand Down
6 changes: 1 addition & 5 deletions tests/test_heartbeat.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from datetime import datetime, timedelta, timezone
import json
import logging
from datetime import datetime, timedelta

from aw_core.models import Event
from aw_transform import heartbeat_merge, heartbeat_reduce

import unittest


def test_heartbeat_merge():
"""Events should merge"""
Expand Down
Loading

0 comments on commit 085d823

Please sign in to comment.