Skip to content

Commit 2c81d44

Browse files
committed
mypy
1 parent d3c5461 commit 2c81d44

4 files changed

Lines changed: 13 additions & 7 deletions

File tree

qualtran/bloqs/for_testing/costing.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@
1919
from qualtran.resource_counting import BloqCountT, CostKey, SympySymbolAllocator
2020

2121

22+
def _convert_callees(callees: Sequence[BloqCountT]) -> Tuple[BloqCountT, ...]:
23+
# Convert to tuples in a type-checked way.
24+
return tuple(callees)
25+
26+
2227
@frozen
2328
class CostingBloq(Bloq):
2429
"""A bloq that lets you set the costs via attributes."""
2530

2631
name: str
2732
num_qubits: int
28-
callees: Sequence[BloqCountT] = field(converter=tuple, factory=tuple)
33+
callees: Sequence[BloqCountT] = field(converter=_convert_callees, factory=tuple)
2934
static_costs: Sequence[Tuple[CostKey, Any]] = field(converter=tuple, factory=tuple)
3035

3136
@property

qualtran/resource_counting/_call_graph.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""Functionality for the `Bloq.call_graph()` protocol."""
1616

17-
import collections.abc as abc
17+
import collections.abc
1818
from collections import defaultdict
1919
from typing import Callable, Dict, List, Optional, Sequence, Set, Tuple, Union
2020

@@ -231,7 +231,7 @@ def get_bloq_call_graph(
231231
keep = lambda b: False
232232
if generalizer is None:
233233
generalizer = lambda b: b
234-
if isinstance(generalizer, abc.Sequence):
234+
if isinstance(generalizer, collections.abc.Sequence):
235235
generalizer = _make_composite_generalizer(*generalizer)
236236

237237
g = nx.DiGraph()

qualtran/resource_counting/_costing.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import abc
16+
import collections
1617
import logging
1718
import time
1819
from collections import defaultdict
@@ -173,7 +174,7 @@ def get_cost_value(
173174
costs_cache = {}
174175
if generalizer is None:
175176
generalizer = lambda b: b
176-
if isinstance(generalizer, (list, tuple)):
177+
if isinstance(generalizer, collections.abc.Sequence):
177178
generalizer = _make_composite_generalizer(*generalizer)
178179

179180
cost_val = _get_cost_value(bloq, cost_key, costs_cache=costs_cache, generalizer=generalizer)
@@ -210,7 +211,7 @@ def get_cost_cache(
210211
costs_cache = {}
211212
if generalizer is None:
212213
generalizer = lambda b: b
213-
if isinstance(generalizer, (list, tuple)):
214+
if isinstance(generalizer, collections.abc.Sequence):
214215
generalizer = _make_composite_generalizer(*generalizer)
215216

216217
_get_cost_value(bloq, cost_key, costs_cache=costs_cache, generalizer=generalizer)
@@ -240,7 +241,7 @@ def query_costs(
240241
A dictionary of dictionaries forming a table of multiple costs for multiple bloqs.
241242
This is indexed by bloq, then cost key.
242243
"""
243-
costs = defaultdict(dict)
244+
costs: Dict['Bloq', Dict[CostKey, CostValT]] = defaultdict(dict)
244245
for cost_key in cost_keys:
245246
cost_for_bloqs = get_cost_cache(bloq, cost_key, generalizer=generalizer)
246247
for bloq, val in cost_for_bloqs.items():

qualtran/resource_counting/_costing_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def compute(self, bloq: 'Bloq', get_callee_cost: Callable[['Bloq'], int]) -> int
4343

4444
return total
4545

46-
def zero(self) -> CostValT:
46+
def zero(self) -> int:
4747
return 0
4848

4949
def __hash__(self):

0 commit comments

Comments
 (0)