Skip to content

Prefer covariant types for function parameters #83

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions mcbackend/adapters/pymc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
import base64
import pickle
from typing import Dict, List, Optional, Sequence, Tuple
from typing import List, Mapping, Optional, Sequence, Tuple, Union

import hagelkorn
import numpy
Expand Down Expand Up @@ -59,8 +59,8 @@ def __init__(
*,
from_trace: BaseTrace,
length: int,
draws: Dict[str, numpy.ndarray],
stats: Sequence[Dict[str, numpy.ndarray]],
draws: Mapping[str, numpy.ndarray],
stats: Sequence[Mapping[str, numpy.ndarray]],
):
self._length = length
self._draws = draws
Expand Down Expand Up @@ -105,7 +105,7 @@ def __init__( # pylint: disable=W0622
self,
backend: Backend,
*,
name: str = None,
name: Optional[str] = None,
model=None,
vars=None,
test_point=None,
Expand All @@ -129,7 +129,7 @@ def setup(
self,
draws: int,
chain: int,
sampler_vars: Optional[List[Dict[str, numpy.dtype]]] = None,
sampler_vars: Optional[Sequence[Mapping[str, Union[type, numpy.dtype]]]] = None,
) -> None:
super().setup(draws, chain, sampler_vars)
self.chain = chain
Expand Down
4 changes: 2 additions & 2 deletions mcbackend/backends/clickhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import time
from datetime import datetime, timezone
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple
from typing import Any, Callable, Dict, List, Mapping, Optional, Sequence, Tuple

import clickhouse_driver
import numpy
Expand Down Expand Up @@ -165,7 +165,7 @@ def __init__(
super().__init__(cmeta, rmeta)

def append(
self, draw: Dict[str, numpy.ndarray], stats: Optional[Dict[str, numpy.ndarray]] = None
self, draw: Mapping[str, numpy.ndarray], stats: Optional[Mapping[str, numpy.ndarray]] = None
):
stat = {f"__stat_{sname}": svals for sname, svals in (stats or {}).items()}
params: Dict[str, numpy.ndarray] = {**draw, **stat}
Expand Down
8 changes: 4 additions & 4 deletions mcbackend/backends/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This backend holds draws in memory, managing them via NumPy arrays.
"""
import math
from typing import Dict, List, Optional, Sequence, Tuple
from typing import Dict, List, Mapping, Optional, Sequence, Tuple

import numpy

Expand All @@ -12,8 +12,8 @@

def grow_append(
storage_dict: Dict[str, numpy.ndarray],
values: Dict[str, numpy.ndarray],
rigid: Dict[str, bool],
values: Mapping[str, numpy.ndarray],
rigid: Mapping[str, bool],
draw_idx: int,
):
"""Writes values into storage arrays, growing them if needed."""
Expand Down Expand Up @@ -76,7 +76,7 @@ def __init__(self, cmeta: ChainMeta, rmeta: RunMeta, *, preallocate: int) -> Non
super().__init__(cmeta, rmeta)

def append(
self, draw: Dict[str, numpy.ndarray], stats: Optional[Dict[str, numpy.ndarray]] = None
self, draw: Mapping[str, numpy.ndarray], stats: Optional[Mapping[str, numpy.ndarray]] = None
):
grow_append(self._samples, draw, self._var_is_rigid, self._draw_idx)
if stats:
Expand Down
13 changes: 11 additions & 2 deletions mcbackend/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
"""
import collections
import logging
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Sized, TypeVar
from typing import (
TYPE_CHECKING,
Dict,
List,
Mapping,
Optional,
Sequence,
Sized,
TypeVar,
)

import numpy

Expand Down Expand Up @@ -58,7 +67,7 @@ def __init__(self, cmeta: ChainMeta, rmeta: RunMeta) -> None:
super().__init__()

def append(
self, draw: Dict[str, numpy.ndarray], stats: Optional[Dict[str, numpy.ndarray]] = None
self, draw: Mapping[str, numpy.ndarray], stats: Optional[Mapping[str, numpy.ndarray]] = None
):
"""Appends an iteration to the chain.

Expand Down