Skip to content

Commit 7637a0e

Browse files
author
uri.akavia
committed
corrected according to changes. Updated release notes.
1 parent 0e3fb08 commit 7637a0e

File tree

8 files changed

+139
-384
lines changed

8 files changed

+139
-384
lines changed

.gitignore

-9
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,6 @@ ipython_config.py
8787
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
8888

8989
# User-specific stuff
90-
.idea/**/workspace.xml
91-
.idea/**/tasks.xml
92-
.idea/**/usage.statistics.xml
93-
.idea/**/dictionaries
94-
.idea/**/shelf
9590
.idea/
9691

9792
# Generated files
@@ -235,10 +230,6 @@ docs/_build/
235230
.pybuilder/
236231
target/
237232

238-
# Jupyter Notebook
239-
240-
# IPython
241-
242233
# pyenv
243234
# For a library or package, you might want to ignore these files since the code is
244235
# intended to run in multiple environments; otherwise, check them in:

release-notes/next-release.md

+4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ of SBO:0000633 (see https://sourceforge.net/p/sbo/term-request/113/)
1313
## Other
1414

1515
* Resolve `flake8` issues and add missing type annotations and docstrings in `src/cobra/io` and `tests/test_io` (#1212).
16+
* Updated model.py and test_model.py to Python 3.6+, including type annotations and docstrings.
1617

1718
## Deprecated features
1819

1920
## Backwards incompatible changes
21+
* Removed `model.add_reaction()` and replaced remaining usages of it with `model.add_reactions()`
22+
* Removed the following tests: test_add_remove_reaction_benchmark, test_add_reaction, test_add_reaction_context, test_add_reaction_from_other_model, test_add_cobra_reaction
23+
* Removed `model.__add__` and `model.__iadd__` - use `model.merge` to replace them.

src/cobra/core/dictlist.py

+28-24
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
Pattern,
1313
Tuple,
1414
Type,
15+
TypeVar,
1516
Union,
1617
)
1718

1819
from numpy import bool_
1920

20-
from cobra.core.object import Object
21+
from .object import Object
22+
23+
24+
Object_T = TypeVar("Object_T", bound=Object)
2125

2226

2327
class DictList(list):
@@ -50,12 +54,12 @@ def __init__(self, *args):
5054
self.extend(other)
5155

5256
# noinspection PyShadowingBuiltins
53-
def has_id(self, id: Union[Object, str]) -> bool:
57+
def has_id(self, id: Union[Object_T, str]) -> bool:
5458
"""Check if id is in DictList."""
5559
return id in self._dict
5660

5761
# noinspection PyShadowingBuiltins
58-
def _check(self, id: Union[Object, str]) -> None:
62+
def _check(self, id: Union[Object_T, str]) -> None:
5963
"""Make sure duplicate id's are not added.
6064
6165
This function is called before adding in elements.
@@ -69,15 +73,15 @@ def _generate_index(self) -> None:
6973
self._dict = {v.id: k for k, v in enumerate(self)}
7074

7175
# noinspection PyShadowingBuiltins
72-
def get_by_id(self, id: Union[Object, str]) -> Object:
76+
def get_by_id(self, id: Union[Object_T, str]) -> Object_T:
7377
"""Return the element with a matching id."""
7478
return list.__getitem__(self, self._dict[id])
7579

7680
def list_attr(self, attribute: str) -> list:
7781
"""Return a list of the given attribute for every object."""
7882
return [getattr(i, attribute) for i in self]
7983

80-
def get_by_any(self, iterable: List[Union[str, Object, int]]) -> list:
84+
def get_by_any(self, iterable: List[Union[str, Object_T, int]]) -> list:
8185
"""Get a list of members using several different ways of indexing.
8286
8387
Parameters
@@ -168,29 +172,29 @@ def select_attribute(x: Optional[Any]) -> Any:
168172
results._extend_nocheck(matches)
169173
return results
170174

171-
def _replace_on_id(self, new_object: Object) -> None:
175+
def _replace_on_id(self, new_object: Object_T) -> None:
172176
"""Replace an object by another with the same id."""
173177
the_id = new_object.id
174178
the_index = self._dict[the_id]
175179
list.__setitem__(self, the_index, new_object)
176180

177181
# overriding default list functions with new ones
178-
def append(self, entity: Object) -> None:
182+
def append(self, entity: Object_T) -> None:
179183
"""Append object to end."""
180184
the_id = entity.id
181185
self._check(the_id)
182186
self._dict[the_id] = len(self)
183187
list.append(self, entity)
184188

185-
def union(self, iterable: Iterable[Object]) -> None:
189+
def union(self, iterable: Iterable[Object_T]) -> None:
186190
"""Add elements with id's not already in the model."""
187191
_dict = self._dict
188192
append = self.append
189193
for i in iterable:
190194
if i.id not in _dict:
191195
append(i)
192196

193-
def extend(self, iterable: Iterable[Object]) -> None:
197+
def extend(self, iterable: Iterable[Object_T]) -> None:
194198
"""Extend list by appending elements from the iterable.
195199
196200
Sometimes during initialization from an older pickle, _dict
@@ -223,7 +227,7 @@ def extend(self, iterable: Iterable[Object]) -> None:
223227
f"Is it present twice?"
224228
)
225229

226-
def _extend_nocheck(self, iterable: Iterable[Object]) -> None:
230+
def _extend_nocheck(self, iterable: Iterable[Object_T]) -> None:
227231
"""Extend without checking for uniqueness.
228232
229233
This function should only be used internally by DictList when it
@@ -245,7 +249,7 @@ def _extend_nocheck(self, iterable: Iterable[Object]) -> None:
245249
for i, obj in enumerate(islice(self, current_length, None), current_length):
246250
_dict[obj.id] = i
247251

248-
def __sub__(self, other: Iterable[Object]) -> "DictList":
252+
def __sub__(self, other: Iterable[Object_T]) -> "DictList":
249253
"""Remove a value or values, and returns the new DictList.
250254
251255
x.__sub__(y) <==> x - y
@@ -265,7 +269,7 @@ def __sub__(self, other: Iterable[Object]) -> "DictList":
265269
total.remove(item)
266270
return total
267271

268-
def __isub__(self, other: Iterable[Object]) -> "DictList":
272+
def __isub__(self, other: Iterable[Object_T]) -> "DictList":
269273
"""Remove a value or values in place.
270274
271275
x.__sub__(y) <==> x -= y
@@ -279,7 +283,7 @@ def __isub__(self, other: Iterable[Object]) -> "DictList":
279283
self.remove(item)
280284
return self
281285

282-
def __add__(self, other: Iterable[Object]) -> "DictList":
286+
def __add__(self, other: Iterable[Object_T]) -> "DictList":
283287
"""Add item while returning a new DictList.
284288
285289
x.__add__(y) <==> x + y
@@ -295,7 +299,7 @@ def __add__(self, other: Iterable[Object]) -> "DictList":
295299
total.extend(other)
296300
return total
297301

298-
def __iadd__(self, other: Iterable[Object]) -> "DictList":
302+
def __iadd__(self, other: Iterable[Object_T]) -> "DictList":
299303
"""Add item while returning the same DictList.
300304
301305
x.__iadd__(y) <==> x += y
@@ -337,7 +341,7 @@ def __setstate__(self, state: dict) -> None:
337341
self._generate_index()
338342

339343
# noinspection PyShadowingBuiltins
340-
def index(self, id: Union[str, Object], *args) -> int:
344+
def index(self, id: Union[str, Object_T], *args) -> int:
341345
"""Determine the position in the list.
342346
343347
Parameters
@@ -361,7 +365,7 @@ def index(self, id: Union[str, Object], *args) -> int:
361365
except KeyError:
362366
raise ValueError(f"{str(id)} not found")
363367

364-
def __contains__(self, entity: Union[str, Object]) -> bool:
368+
def __contains__(self, entity: Union[str, Object_T]) -> bool:
365369
"""Ask if the DictList contain an entity.
366370
367371
DictList.__contains__(entity) <==> entity in DictList
@@ -385,7 +389,7 @@ def __copy__(self) -> "DictList":
385389
the_copy._dict = self._dict.copy()
386390
return the_copy
387391

388-
def insert(self, index: int, entity: Object) -> None:
392+
def insert(self, index: int, entity: Object_T) -> None:
389393
"""Insert entity before index."""
390394
self._check(entity.id)
391395
list.insert(self, index, entity)
@@ -396,7 +400,7 @@ def insert(self, index: int, entity: Object) -> None:
396400
_dict[i] = j + 1
397401
_dict[entity.id] = index
398402

399-
def pop(self, *args) -> Object:
403+
def pop(self, *args) -> Object_T:
400404
"""Remove and return item at index (default last)."""
401405
value = list.pop(self, *args)
402406
index = self._dict.pop(value.id)
@@ -410,11 +414,11 @@ def pop(self, *args) -> Object:
410414
_dict[i] = j - 1
411415
return value
412416

413-
def add(self, x: Object) -> None:
417+
def add(self, x: Object_T) -> None:
414418
"""Opposite of `remove`. Mirrors set.add."""
415419
self.extend([x])
416420

417-
def remove(self, x: Union[str, Object]) -> None:
421+
def remove(self, x: Union[str, Object_T]) -> None:
418422
""".. warning :: Internal use only.
419423
420424
Each item is unique in the list which allows this
@@ -446,8 +450,8 @@ def key(i):
446450
self._generate_index()
447451

448452
def __getitem__(
449-
self, i: Union[int, slice, Iterable, Object, "DictList"]
450-
) -> Union["DictList", Object]:
453+
self, i: Union[int, slice, Iterable, Object_T, "DictList"]
454+
) -> Union["DictList", Object_T]:
451455
"""Get item from DictList."""
452456
if isinstance(i, int):
453457
return list.__getitem__(self, i)
@@ -466,7 +470,7 @@ def __getitem__(
466470
else:
467471
return list.__getitem__(self, i)
468472

469-
def __setitem__(self, i: Union[slice, int], y: Union[list, Object]) -> None:
473+
def __setitem__(self, i: Union[slice, int], y: Union[list, Object_T]) -> None:
470474
"""Set an item via index or slice.
471475
472476
Parameters
@@ -512,7 +516,7 @@ def __getslice__(self, i: int, j: int) -> "DictList":
512516
"""Get a slice from it to j of DictList."""
513517
return self.__getitem__(slice(i, j))
514518

515-
def __setslice__(self, i: int, j: int, y: Union[list, Object]) -> None:
519+
def __setslice__(self, i: int, j: int, y: Union[list, Object_T]) -> None:
516520
"""Set slice, where y is an iterable."""
517521
self.__setitem__(slice(i, j), y)
518522

0 commit comments

Comments
 (0)