Skip to content

Commit 5a33495

Browse files
committed
apply suggested converter method
also disable mypy checks for int/string env conversion tests
1 parent 7ad8f24 commit 5a33495

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/psij/job_spec.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ def _to_path(arg: Union[str, pathlib.Path, None]) -> Optional[pathlib.Path]:
2121
return pathlib.Path(arg)
2222

2323

24+
def _to_env_dict(arg: Union[Dict[str, Union[str, int]], None]) -> Dict[str, str]:
25+
if arg is None:
26+
return dict()
27+
ret = dict()
28+
for k,v in arg.items():
29+
if isinstance(v, int):
30+
ret[k] = str(v)
31+
else:
32+
ret[k] = v
33+
return ret
34+
35+
2436
class JobSpec(object):
2537
"""A class that describes the details of a job."""
2638

@@ -125,7 +137,7 @@ def __init__(self, executable: Optional[str] = None, arguments: Optional[List[st
125137
# care of the conversion, but mypy gets confused
126138
self._directory = _to_path(directory)
127139
self.inherit_environment = inherit_environment
128-
self.environment = environment
140+
self.environment = _to_env_dict(environment)
129141
self._stdin_path = _to_path(stdin_path)
130142
self._stdout_path = _to_path(stdout_path)
131143
self._stderr_path = _to_path(stderr_path)
@@ -160,15 +172,7 @@ def environment(self) -> Optional[Dict[str, str]]:
160172
@environment.setter
161173
def environment(self, env: Optional[Dict[str, Union[str, int]]]) -> None:
162174
"""Ensure env dict values to be string typed."""
163-
self._environment = None
164-
165-
if env:
166-
self._environment = {}
167-
for k, v in env.items():
168-
if isinstance(v, int):
169-
self._environment[k] = str(v)
170-
else:
171-
self._environment[k] = v
175+
self._environment = _to_env_dict(env)
172176

173177
@property
174178
def directory(self) -> Optional[pathlib.Path]:

tests/test_executor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def test_env_var(execparams: ExecutorTestParams) -> None:
126126
arguments=['-c', 'env > /tmp/t; echo -n $TEST_VAR$TEST_INT'],
127127
stdout_path=outp))
128128
assert job.spec is not None
129-
job.spec.environment = {'TEST_INT': 1, 'TEST_VAR': '_y_'}
129+
job.spec.environment = {'TEST_INT': 1, 'TEST_VAR': '_y_'} # type: ignore
130130
ex = _get_executor_instance(execparams, job)
131131
ex.submit(job)
132132
status = job.wait(timeout=_get_timeout(execparams))

tests/test_job_spec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_environment_types() -> None:
3232
spec.environment = {'foo': 'bar'}
3333
assert spec.environment['foo'] == 'bar'
3434

35-
spec.environment = {'foo': 'biz', 'bar': 42}
35+
spec.environment = {'foo': 'biz', 'bar': 42} # type: ignore
3636
assert spec.environment['foo'] == 'biz'
3737
assert spec.environment['bar'] == '42'
3838

0 commit comments

Comments
 (0)