Skip to content

Commit

Permalink
Revise typing
Browse files Browse the repository at this point in the history
Revises type annotations to reflect new minimum Python
version as 3.10.
  • Loading branch information
maread99 committed Sep 23, 2024
1 parent 8d5d7e6 commit e349842
Show file tree
Hide file tree
Showing 15 changed files with 21 additions and 49 deletions.
12 changes: 6 additions & 6 deletions src/market_analy/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
from abc import ABCMeta
from collections.abc import Callable, Sequence
from datetime import datetime
from typing import TYPE_CHECKING, Any, Literal, Union
from typing import TYPE_CHECKING, Any, Literal

import market_prices as mp
import matplotlib as mpl
Expand Down Expand Up @@ -113,9 +113,9 @@
from .trends.guis import TrendsGui, TrendsGuiBase
from .trends.analy import Trends

Date = Union[str, datetime, None]
Calendar = Union[str, ExchangeCalendar]
Symbols = Union[str, list[str]]
Date = str | datetime | None
Calendar = str | ExchangeCalendar
Symbols = str | list[str]
ChartEngine = Literal["bqplot", "matplotlib"]


Expand Down Expand Up @@ -184,8 +184,8 @@ def add_summary(
return pd.concat([rtrn, summary_column], axis=1)


SummaryRowFuncDef = tuple[str, Union[str, list[str]]]
SummaryRowDef = Union[SummaryRowFuncDef, list[SummaryRowFuncDef]]
SummaryRowFuncDef = tuple[str, str | list[str]]
SummaryRowDef = SummaryRowFuncDef | list[SummaryRowFuncDef]


def add_summary_row(
Expand Down
2 changes: 0 additions & 2 deletions src/market_analy/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
navigating between cases.
"""

from __future__ import annotations

from abc import ABC, abstractmethod
from collections.abc import Sequence
from dataclasses import dataclass
Expand Down
19 changes: 8 additions & 11 deletions src/market_analy/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,14 @@
Bar Chart displaying precentage changes of multiple instruments.
"""

from __future__ import annotations

from abc import ABCMeta, abstractmethod
from collections import defaultdict
from collections.abc import Callable, Sequence
from copy import copy, deepcopy
import enum
from functools import lru_cache, partialmethod
import itertools
import typing
from typing import Any, Literal
from typing import Any, Literal, TYPE_CHECKING

import bqplot as bq
import IPython
Expand Down Expand Up @@ -93,17 +90,17 @@ class Groups(enum.Enum):
CASES_GROUPS = set([Groups.CASES_SCATTERS]) | CASES_OTHER

# type aliases
AddedMarkKeys = typing.Union[
str,
Literal[
AddedMarkKeys = (
str
| Literal[
Groups.CASE,
Groups.PERSIST,
Groups.CASES_SCATTERS,
Groups.CASES_OTHER_0,
Groups.CASES_OTHER_1,
Groups.CASES_OTHER_2,
],
]
]
)
AddedMarks = dict[AddedMarkKeys, list[bq.Mark]]
AxesKwargs = dict[ubq.ScaleKeys, dict[str, Any]]

Expand Down Expand Up @@ -162,7 +159,7 @@ class Base(metaclass=ABCMeta):
Data represented by x-axis. Subclass can optionally expose
via more concrete property.
_y_data: Union[DataFrame, Series]
_y_data: DataFrame | Series
Data represented by y-axis. Subclass can optionally expose
via more concrete property.
Expand Down Expand Up @@ -3123,7 +3120,7 @@ def get_next_case(self) -> CaseSupportsChartAnaly:
case = self.current_case
i = 0 if case is None else self.cases.get_index(case) + 1
if i == len(self.cases.cases):
if typing.TYPE_CHECKING:
if TYPE_CHECKING:
assert case is not None
return case # current case is last case
return self.cases.cases[i]
Expand Down
2 changes: 0 additions & 2 deletions src/market_analy/gui_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
Percent change bar chart options for multiple instruments.
"""

from __future__ import annotations

from collections.abc import Callable
import re

Expand Down
11 changes: 5 additions & 6 deletions src/market_analy/standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import datetime
from typing import Union

import pandas as pd
import valimp
Expand Down Expand Up @@ -100,7 +99,7 @@ def net_of(data: pd.DataFrame, rebase: bool = True) -> pd.DataFrame:
return pd.DataFrame({col: mapping[col] for col in df.columns})


def get_ath(data: Union[pd.Series, pd.DataFrame]) -> Union[pd.Series, pd.DataFrame]:
def get_ath(data: pd.Series | pd.DataFrame) -> pd.Series | pd.DataFrame:
"""Return all-time high as at each timestamp.
Returns ATH as at end of period represented by corresponding
Expand Down Expand Up @@ -129,10 +128,10 @@ def get_ath(data: Union[pd.Series, pd.DataFrame]) -> Union[pd.Series, pd.DataFra

@valimp.parse
def get_period_high(
data: Union[pd.Series, pd.DataFrame],
window: Union[int, str, datetime.timedelta],
data: pd.Series | pd.DataFrame,
window: int | str | datetime.timedelta,
include_current: bool = True,
) -> Union[pd.Series, pd.DataFrame]:
) -> pd.Series | pd.DataFrame:
"""Return 52wk high as at each timestamp.
NB: For sessions within the first 52 weeks of data, the high will
Expand Down Expand Up @@ -186,7 +185,7 @@ def get_period_high(
@valimp.parse
def get_pct_off_high(
data: pd.DataFrame,
window: Union[int, str, datetime.timedelta, None],
window: int | str | datetime.timedelta | None,
include_current: bool = True,
) -> pd.DataFrame:
"""Return percent off high as at each timestamp.
Expand Down
2 changes: 0 additions & 2 deletions src/market_analy/trends/charts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Charts for trend analysis."""

from __future__ import annotations

import typing
from collections.abc import Callable
from functools import cached_property, partialmethod
Expand Down
2 changes: 0 additions & 2 deletions src/market_analy/utils/bq_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Utility constants, functions and classes for bqplot."""

from __future__ import annotations

from collections.abc import Iterable
from copy import copy
from itertools import cycle
Expand Down
4 changes: 1 addition & 3 deletions src/market_analy/utils/ipyvuetify_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Utility functions and classes for ipyvuetify library."""

from __future__ import annotations

from collections.abc import Callable
from typing import Literal

Expand Down Expand Up @@ -402,7 +400,7 @@ class ToggleIcons(SelectableList):
Attributes
----------
contain_me: List[Union[IconBut, vue.Tooltip]]
contain_me: List[IconBut | vue.Tooltip]
To house `ToggleIcons` in a parent component, pass `.contain_me` to
parent component's children attribute.
Expand Down
2 changes: 0 additions & 2 deletions src/market_analy/utils/ipywidgets_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
Double handled slider to select date ranges.
"""

from __future__ import annotations

import asyncio
from collections.abc import Callable
from contextlib import contextmanager
Expand Down
2 changes: 0 additions & 2 deletions src/market_analy/utils/list_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Utility functions and classes for lists."""

from __future__ import annotations

from typing import Any, Literal
from copy import copy

Expand Down
2 changes: 0 additions & 2 deletions src/market_analy/utils/maths_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Mathematics utility functions and classes."""

from __future__ import annotations

import math


Expand Down
2 changes: 0 additions & 2 deletions src/market_analy/utils/mkt_prices_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Utility funcions for market_prices library."""

from __future__ import annotations

import pandas as pd
import market_prices as mp

Expand Down
2 changes: 0 additions & 2 deletions src/market_analy/utils/pandas_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Utilities for operating on pandas DataFrame and Series."""

from __future__ import annotations

from typing import Any, Literal
from zoneinfo import ZoneInfo

Expand Down
2 changes: 0 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Common pytest fixtures and test utility functions."""

from __future__ import annotations

from collections import abc
import pathlib
import pickle
Expand Down
4 changes: 1 addition & 3 deletions tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
return from one or two specific calls against hard-coded expected returns.
"""

from __future__ import annotations

from collections import abc
import contextlib
import io
Expand Down Expand Up @@ -828,7 +826,7 @@ def test_max_dec(self, analy, daily_pp, tz):
def test_corr(self, analy, analy_other):
f = analy.corr
rtrn = f(analy_other, style=False, end="2022-12-31", days=20)
assert rtrn == 0.49620301268372446
assert round(rtrn, 11) == 0.49620301268

rtrn = f(analy_other, end="2022-12-31", days=20)
expected = "\\begin{table}\n\\caption{Correlation 20D to 2022-12-31 (2022-12-01 to 30)}\n\\begin{tabular}{lr}\n & BARC.L \\\\\nAZN.L & \\background-color#6bacd1 \\color#f1f1f1 0.50 \\\\\n\\end{tabular}\n\\end{table}\n"
Expand Down

0 comments on commit e349842

Please sign in to comment.