Skip to content

Commit 334a036

Browse files
committed
Small improvements in scoping and type assertions
1 parent c4fce6f commit 334a036

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/minizinc/instance.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,14 @@ async def solve_async(
236236
237237
"""
238238
status = Status.UNKNOWN
239-
solution = None
240239
statistics: Dict[str, Any] = {}
241240

242241
multiple_solutions = (
243242
all_solutions or intermediate_solutions or nr_solutions is not None
244243
)
245-
if multiple_solutions:
246-
solution = []
244+
solution: Union[Optional[Any], List[Any]] = (
245+
[] if multiple_solutions else None
246+
)
247247

248248
async for result in self.solutions(
249249
time_limit=time_limit,
@@ -261,7 +261,7 @@ async def solve_async(
261261
statistics.update(result.statistics)
262262
if result.solution is not None:
263263
if multiple_solutions:
264-
assert solution is not None
264+
assert isinstance(solution, list)
265265
solution.append(result.solution)
266266
else:
267267
solution = result.solution
@@ -302,12 +302,12 @@ async def diverse_solutions(
302302
"mzn-analyse executable could not be located"
303303
)
304304

305-
try:
306-
# Create a temporary file in which the diversity model (generated by mzn-analyse) is placed
307-
div_file = tempfile.NamedTemporaryFile(
308-
prefix="mzn_div", suffix=".mzn", delete=False
309-
)
305+
# Create a temporary file in which the diversity model (generated by mzn-analyse) is placed
306+
div_file = tempfile.NamedTemporaryFile(
307+
prefix="mzn_div", suffix=".mzn", delete=False
308+
)
310309

310+
try:
311311
# Extract the diversity annotations.
312312
with self.files() as files:
313313
div_anns = mzn_analyse.run(
@@ -610,12 +610,11 @@ async def solutions(
610610

611611
# Run the MiniZinc process
612612
proc = await self._driver._create_process(cmd, solver=solver)
613-
try:
614-
assert isinstance(proc.stderr, asyncio.StreamReader)
615-
assert isinstance(proc.stdout, asyncio.StreamReader)
616-
617-
read_stderr = asyncio.create_task(_read_all(proc.stderr))
613+
assert isinstance(proc.stderr, asyncio.StreamReader)
614+
assert isinstance(proc.stdout, asyncio.StreamReader)
615+
read_stderr = asyncio.create_task(_read_all(proc.stderr))
618616

617+
try:
619618
async for obj in decode_async_json_stream(
620619
proc.stdout, cls=MZNJSONDecoder, enum_map=self._enum_map
621620
):
@@ -850,6 +849,7 @@ def analyse(self):
850849
if obj["type"] == "interface":
851850
interface = obj
852851
break
852+
assert interface is not None
853853
old_method = self._method_cache
854854
self._method_cache = Method.from_string(interface["method"])
855855
self._input_cache = {}
@@ -1033,6 +1033,7 @@ def _parse_stream_obj(self, obj, statistics):
10331033
if "_checker" in statistics:
10341034
tmp["_checker"] = statistics.pop("_checker")
10351035

1036+
assert self.output_type is not None
10361037
solution = self.output_type(**tmp)
10371038
statistics["time"] = timedelta(milliseconds=obj["time"])
10381039
elif obj["type"] == "time":

0 commit comments

Comments
 (0)