Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CU-8692uznvd: Allow empty-dict config.linking.filters.cuis and convert to set in memory #352

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion medcat/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from pydantic import BaseModel, Extra, ValidationError
from pydantic import BaseModel, Extra, ValidationError, validator
from pydantic.dataclasses import Any, Callable, Dict, Optional, Union
from pydantic.fields import ModelField
from typing import List, Set, Tuple, cast
Expand Down Expand Up @@ -433,6 +433,19 @@ class LinkingFilters(MixingConfig, BaseModel):
cuis: Set[str] = set()
cuis_exclude: Set[str] = set()

@validator("cuis", pre=True, always=True)
def convert_empty_dict_to_set(cls, value):
if isinstance(value, dict) and not value:
# is empty dict
logger.warning("Loading an old model where "
"config.linking.filters.cuis has been "
"dict to an empty dict instead of an empty "
"set. Converting the dict to a set in memory "
"as that is what is expected. Please consider "
"saving the model again.")
return set()
return value

def check_filters(self, cui: str) -> bool:
"""Checks is a CUI in the filters

Expand Down
21 changes: 20 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import pickle
import tempfile
from medcat.config import Config, MixingConfig, VersionInfo, General
from medcat.config import Config, MixingConfig, VersionInfo, General, LinkingFilters
from pydantic import ValidationError
import os

Expand Down Expand Up @@ -180,5 +180,24 @@ def test_from_dict(self):
self.assertEqual("value", config.key)


class ConfigLinkingFiltersTests(unittest.TestCase):

def test_allows_empty_dict_for_cuis(self):
lf = LinkingFilters(cuis={})
self.assertIsNotNone(lf)

def test_empty_dict_converted_to_empty_set(self):
lf = LinkingFilters(cuis={})
self.assertEqual(lf.cuis, set())

def test_not_allow_nonempty_dict_for_cuis(self):
with self.assertRaises(ValidationError):
LinkingFilters(cuis={"KEY": "VALUE"})

def test_not_allow_empty_dict_for_cuis_exclude(self):
with self.assertRaises(ValidationError):
LinkingFilters(cuis_exclude={})


if __name__ == '__main__':
unittest.main()
Loading