From 9ae00c3adc083b7ea9689e786ca63fbaaa4092ad Mon Sep 17 00:00:00 2001 From: James Stevenson Date: Fri, 31 Jan 2025 12:50:25 -0500 Subject: [PATCH 1/2] featremove unsafe uses of assert --- src/ga4gh/core/enderef.py | 4 +++- src/ga4gh/vrs/dataproxy.py | 3 ++- src/ga4gh/vrs/normalize.py | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ga4gh/core/enderef.py b/src/ga4gh/core/enderef.py index 7808d2b0..17319107 100644 --- a/src/ga4gh/core/enderef.py +++ b/src/ga4gh/core/enderef.py @@ -48,7 +48,9 @@ def _enref(o): elif isinstance(v, str): pass elif is_curie_type(v): # already a reference - assert is_ga4gh_identifier(v), "Identifiable attribute CURIE is contains an invalid identifier" + if not is_ga4gh_identifier(v): + msg = "Identifiable attribute CURIE is contains an invalid identifier" + raise TypeError(msg) elif v is not None: _id = _id_and_store(v) if _id: diff --git a/src/ga4gh/vrs/dataproxy.py b/src/ga4gh/vrs/dataproxy.py index 0fd4d73b..53cb1e9a 100644 --- a/src/ga4gh/vrs/dataproxy.py +++ b/src/ga4gh/vrs/dataproxy.py @@ -308,7 +308,8 @@ def _isoformat(o): """ # stolen from connexion flask_app.py - assert isinstance(o, datetime.datetime) + if not isinstance(o, datetime.datetime): + raise TypeError if o.tzinfo: # eg: '2015-09-25T23:14:42.588601+00:00' return o.isoformat("T") diff --git a/src/ga4gh/vrs/normalize.py b/src/ga4gh/vrs/normalize.py index 2e37bf60..744390a2 100644 --- a/src/ga4gh/vrs/normalize.py +++ b/src/ga4gh/vrs/normalize.py @@ -277,7 +277,9 @@ def normalize(vo, data_proxy=None, **kwargs): of the `sequence`. To exclude `state.sequence`, set to 0. """ - assert is_pydantic_instance(vo) + if not is_pydantic_instance(vo): + msg = f"Object is of class {vo.__class__} (with parents {vo.__class__.__mro__}), but normalize requires objects that inherit from pydantic.BaseModel." + raise TypeError(msg) vo_type = vo.type if vo_type in handlers: From 70d1420086b75a53e4298618ff8d95f0744c120b Mon Sep 17 00:00:00 2001 From: James Stevenson Date: Fri, 31 Jan 2025 14:05:23 -0500 Subject: [PATCH 2/2] update docstrngs --- src/ga4gh/core/enderef.py | 14 +++++++------- src/ga4gh/vrs/dataproxy.py | 3 +++ src/ga4gh/vrs/normalize.py | 9 ++++++--- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/ga4gh/core/enderef.py b/src/ga4gh/core/enderef.py index 49b75027..ed6af244 100644 --- a/src/ga4gh/core/enderef.py +++ b/src/ga4gh/core/enderef.py @@ -26,13 +26,13 @@ def ga4gh_enref(o, cra_map, object_store=None, return_id_obj_tuple=False) -> tup """Recursively convert "referable attributes" from inlined to referenced form. Returns a new object. - cra_map: class referrable-attribute map; { o.type: [attr, attr, ...] } - - If `object_store` is provided, it must be an - collections.abc.MutableMapping subclass and will be used to store - objects as they are referenced. If `object_store` is not provided - (or None), referenced objects will not be stored. - + :param o: VRS object + :param cra_map: class referrable-attribute map; { o.type: [attr, attr, ...] } + :param object_store: `collections.abc.MutableMapping` subclass that stores + objects as they are referenced. If not given, referenced objects are not stored. + :param return_id_obj_tuple: include ID with return value or not + :return: new, enref'd object, as well as object ID if param set + :raise TypeError: if any object IDs are non-GA4GH CURIEs """ def _id_and_store(o): # noqa: ANN202 ANN001 diff --git a/src/ga4gh/vrs/dataproxy.py b/src/ga4gh/vrs/dataproxy.py index e3d44d5f..ba2b5e2e 100644 --- a/src/ga4gh/vrs/dataproxy.py +++ b/src/ga4gh/vrs/dataproxy.py @@ -320,6 +320,9 @@ def _isoformat(o: datetime.datetime) -> str: >>> _isoformat(dt) '2019-10-15T10:23:41.115927Z' + :param o: date object to format + :return: ISO8601-formatted string equivalent + :raise TypeError: if object given isn't a Python datetime instance """ # stolen from connexion flask_app.py if not isinstance(o, datetime.datetime): diff --git a/src/ga4gh/vrs/normalize.py b/src/ga4gh/vrs/normalize.py index 349e28e2..cc929a4f 100644 --- a/src/ga4gh/vrs/normalize.py +++ b/src/ga4gh/vrs/normalize.py @@ -273,9 +273,12 @@ def _normalize_cis_phased_block(o, data_proxy: _DataProxy | None = None): # noq def normalize(vo, data_proxy: _DataProxy | None = None, **kwargs): """Normalize given vrs object, regardless of type - kwargs: - rle_seq_limit: If RLE is set as the new state, set the limit for the length - of the `sequence`. To exclude `state.sequence`, set to 0. + :param vo: + :param data_proxy: GA4GH sequence dataproxy instance, if needed + :keyword rle_seq_limit: If RLE is set as the new state, set the limit for the length + of the `sequence`. To exclude `state.sequence`, set to 0. + :return: normalized object + :raise TypeError: if given object isn't a pydantic.BaseModel """ if not is_pydantic_instance(vo): msg = f"Object is of class {vo.__class__} (with parents {vo.__class__.__mro__}), but normalize requires objects that inherit from pydantic.BaseModel."