From 4f2bcee7d1a869d651bd6ed4bea2f7134c16657d Mon Sep 17 00:00:00 2001 From: Myst <1592048+LeMyst@users.noreply.github.com> Date: Sat, 24 Jun 2023 09:51:29 +0200 Subject: [PATCH] Upgrade to Python 3.8 (#558) With "pyup-dirs --py38-plus --recursive ..." --- .github/workflows/python-pytest.yaml | 2 +- README.md | 4 +- docs/source/conf.py | 11 ++-- setup.cfg | 3 +- wikibaseintegrator/datatypes/basedatatype.py | 8 +-- wikibaseintegrator/entities/baseentity.py | 49 ++++++++------- wikibaseintegrator/entities/item.py | 12 ++-- wikibaseintegrator/entities/lexeme.py | 22 +++---- wikibaseintegrator/entities/mediainfo.py | 14 ++--- wikibaseintegrator/entities/property.py | 18 +++--- wikibaseintegrator/models/aliases.py | 20 +++---- wikibaseintegrator/models/claims.py | 44 +++++++------- wikibaseintegrator/models/descriptions.py | 4 +- wikibaseintegrator/models/forms.py | 22 +++---- wikibaseintegrator/models/labels.py | 4 +- wikibaseintegrator/models/language_values.py | 30 +++++----- wikibaseintegrator/models/lemmas.py | 4 +- wikibaseintegrator/models/qualifiers.py | 20 +++---- wikibaseintegrator/models/references.py | 30 +++++----- wikibaseintegrator/models/senses.py | 20 +++---- wikibaseintegrator/models/sitelinks.py | 14 ++--- wikibaseintegrator/models/snaks.py | 18 +++--- wikibaseintegrator/wbi_fastrun.py | 56 ++++++++--------- wikibaseintegrator/wbi_helpers.py | 63 ++++++++++---------- wikibaseintegrator/wikibaseintegrator.py | 4 +- 25 files changed, 240 insertions(+), 256 deletions(-) diff --git a/.github/workflows/python-pytest.yaml b/.github/workflows/python-pytest.yaml index be859368..1fd8ed55 100644 --- a/.github/workflows/python-pytest.yaml +++ b/.github/workflows/python-pytest.yaml @@ -27,7 +27,7 @@ jobs: strategy: matrix: - python-version: [ '3.7', '3.8', '3.9', '3.10', '3.11', '3.12-dev' ] + python-version: [ '3.8', '3.9', '3.10', '3.11', '3.12-dev' ] steps: - uses: actions/checkout@v3.5.3 diff --git a/README.md b/README.md index e9059674..6ff267db 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ The main differences between these two libraries are : * A complete rewrite of the library with a more object-oriented architecture allowing for easy interaction, data validation and extended functionality * Add support for reading and writing Lexeme, MediaInfo and Property entities -* Python 3.7 to 3.11 support, validated with unit tests +* Python 3.8 to 3.11 support, validated with unit tests * Type hints implementation for arguments and return, checked with mypy static type checker * Add OAuth 2.0 login method * Add logging module support @@ -95,7 +95,7 @@ Here is a list of different projects that use the library: # Installation # The easiest way to install WikibaseIntegrator is to use the `pip` package manager. WikibaseIntegrator supports Python -3.7 and above. If Python 2 is installed, `pip` will lead to an error indicating missing dependencies. +3.8 and above. If Python 2 is installed, `pip` will lead to an error indicating missing dependencies. ```bash python -m pip install wikibaseintegrator diff --git a/docs/source/conf.py b/docs/source/conf.py index b047f35e..8572fcef 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the @@ -44,18 +43,18 @@ master_doc = 'index' # General information about the project. -project = u'WikibaseIntegrator' -copyright = u'%d, LeMyst' % datetime.now().year -author = u'LeMyst and WikibaseIntegrator contributors' +project = 'WikibaseIntegrator' +copyright = f'{datetime.now().year}, LeMyst' +author = 'LeMyst and WikibaseIntegrator contributors' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = u'0.12.5.dev0' +version = '0.12.5.dev0' # The full version, including alpha/beta/rc tags. -release = u'0.12.5.dev0' +release = '0.12.5.dev0' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/setup.cfg b/setup.cfg index 47805cde..cb904dd2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -16,7 +16,6 @@ long_description_content_type = text/markdown platform = any classifiers = Programming Language :: Python - Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 Programming Language :: Python :: 3.10 @@ -41,7 +40,7 @@ install_requires = requests>=2.27.1,<2.29.0 requests-oauthlib~=1.3.1 ujson>=5.4,<5.6 -python_requires = >=3.7, <3.13 +python_requires = >=3.8, <3.13 [options.extras_require] dev = diff --git a/wikibaseintegrator/datatypes/basedatatype.py b/wikibaseintegrator/datatypes/basedatatype.py index 7b492033..0db1c3d7 100644 --- a/wikibaseintegrator/datatypes/basedatatype.py +++ b/wikibaseintegrator/datatypes/basedatatype.py @@ -1,7 +1,7 @@ from __future__ import annotations import re -from typing import Any, List, Optional, Type, Union +from typing import Any from wikibaseintegrator.models import Claim @@ -11,7 +11,7 @@ class BaseDataType(Claim): The base class for all Wikibase data types, they inherit from it """ DTYPE = 'base-data-type' - subclasses: List[Type[BaseDataType]] = [] + subclasses: list[type[BaseDataType]] = [] sparql_query: str = ''' SELECT * WHERE {{ ?item_id <{wb_url}/prop/{pid}> ?s . @@ -19,7 +19,7 @@ class BaseDataType(Claim): }} ''' - def __init__(self, prop_nr: Optional[Union[int, str]] = None, **kwargs: Any): + def __init__(self, prop_nr: int | str | None = None, **kwargs: Any): """ Constructor, will be called by all data types. @@ -36,7 +36,7 @@ def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) cls.subclasses.append(cls) - def set_value(self, value: Optional[Any] = None): + def set_value(self, value: Any | None = None): pass def get_sparql_value(self) -> str: diff --git a/wikibaseintegrator/entities/baseentity.py b/wikibaseintegrator/entities/baseentity.py index 13ca5f6d..b032da62 100644 --- a/wikibaseintegrator/entities/baseentity.py +++ b/wikibaseintegrator/entities/baseentity.py @@ -2,7 +2,7 @@ import logging from copy import copy -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union +from typing import TYPE_CHECKING, Any from wikibaseintegrator import wbi_fastrun from wikibaseintegrator.datatypes import BaseDataType @@ -20,10 +20,10 @@ class BaseEntity: ETYPE = 'base-entity' - subclasses: List[Type[BaseEntity]] = [] + subclasses: list[type[BaseEntity]] = [] - def __init__(self, api: Optional['WikibaseIntegrator'] = None, title: Optional[str] = None, pageid: Optional[int] = None, lastrevid: Optional[int] = None, - type: Optional[str] = None, id: Optional[str] = None, claims: Optional[Claims] = None, is_bot: Optional[bool] = None, login: Optional[_Login] = None): + def __init__(self, api: WikibaseIntegrator | None = None, title: str | None = None, pageid: int | None = None, lastrevid: int | None = None, type: str | None = None, + id: str | None = None, claims: Claims | None = None, is_bot: bool | None = None, login: _Login | None = None): if not api: from wikibaseintegrator import WikibaseIntegrator self.api = WikibaseIntegrator() @@ -57,30 +57,30 @@ def api(self, value: WikibaseIntegrator): self.__api = value @property - def title(self) -> Optional[str]: + def title(self) -> str | None: return self.__title @title.setter - def title(self, value: Optional[str]): + def title(self, value: str | None): self.__title = value @property - def pageid(self) -> Union[str, int, None]: + def pageid(self) -> str | int | None: return self.__pageid @pageid.setter - def pageid(self, value: Union[str, int, None]): + def pageid(self, value: str | int | None): if isinstance(value, str): - self.__pageid: Union[str, int, None] = int(value) + self.__pageid: str | int | None = int(value) else: self.__pageid = value @property - def lastrevid(self) -> Optional[int]: + def lastrevid(self) -> int | None: return self.__lastrevid @lastrevid.setter - def lastrevid(self, value: Optional[int]): + def lastrevid(self, value: int | None): self.__lastrevid = value @property @@ -92,11 +92,11 @@ def type(self, value: str): self.__type = value @property - def id(self) -> Optional[str]: + def id(self) -> str | None: return self.__id @id.setter - def id(self, value: Optional[str]): + def id(self, value: str | None): self.__id = value @property @@ -109,7 +109,7 @@ def claims(self, value: Claims): raise TypeError self.__claims = value - def add_claims(self, claims: Union[Claim, List[Claim], Claims], action_if_exists: ActionIfExists = ActionIfExists.APPEND_OR_REPLACE) -> BaseEntity: + def add_claims(self, claims: Claim | list[Claim] | Claims, action_if_exists: ActionIfExists = ActionIfExists.APPEND_OR_REPLACE) -> BaseEntity: """ :param claims: A Claim, list of Claim or just a Claims object to add to this Claims object. @@ -125,13 +125,13 @@ def add_claims(self, claims: Union[Claim, List[Claim], Claims], action_if_exists return self - def get_json(self) -> Dict[str, Union[str, Dict[str, List]]]: + def get_json(self) -> dict[str, str | dict[str, list]]: """ To get the dict equivalent of the JSON representation of the entity. :return: """ - json_data: Dict = { + json_data: dict = { 'type': self.type, 'claims': self.claims.get_json() } @@ -140,7 +140,7 @@ def get_json(self) -> Dict[str, Union[str, Dict[str, List]]]: return json_data - def from_json(self, json_data: Dict[str, Any]) -> BaseEntity: + def from_json(self, json_data: dict[str, Any]) -> BaseEntity: """ Import a dictionary into BaseEntity attributes. @@ -163,8 +163,7 @@ def from_json(self, json_data: Dict[str, Any]) -> BaseEntity: return self # noinspection PyMethodMayBeStatic - def _get(self, entity_id: str, login: Optional[_Login] = None, allow_anonymous: bool = True, is_bot: Optional[bool] = None, - **kwargs: Any) -> Dict: # pylint: disable=no-self-use + def _get(self, entity_id: str, login: _Login | None = None, allow_anonymous: bool = True, is_bot: bool | None = None, **kwargs: Any) -> dict: # pylint: disable=no-self-use """ Retrieve an entity in json representation from the Wikibase instance @@ -187,7 +186,7 @@ def _get(self, entity_id: str, login: Optional[_Login] = None, allow_anonymous: return mediawiki_api_call_helper(data=params, login=login, allow_anonymous=allow_anonymous, is_bot=is_bot, **kwargs) - def clear(self, **kwargs: Any) -> Dict[str, Any]: + def clear(self, **kwargs: Any) -> dict[str, Any]: """ Use the `clear` parameter of `wbeditentity` API call to clear the content of the entity. The entity will be updated with an empty dictionary. @@ -197,8 +196,8 @@ def clear(self, **kwargs: Any) -> Dict[str, Any]: """ return self._write(data={}, clear=True, **kwargs) - def _write(self, data: Optional[Dict] = None, summary: Optional[str] = None, login: Optional[_Login] = None, allow_anonymous: bool = False, limit_claims: Optional[List[Union[str, int]]] = None, clear: bool = False, as_new: bool = False, - is_bot: Optional[bool] = None, **kwargs: Any) -> Dict[str, Any]: + def _write(self, data: dict | None = None, summary: str | None = None, login: _Login | None = None, allow_anonymous: bool = False, limit_claims: list[str | int] | None = None, + clear: bool = False, as_new: bool = False, is_bot: bool | None = None, **kwargs: Any) -> dict[str, Any]: """ Writes the entity JSON to the Wikibase instance and after successful write, returns the "entity" part of the response. @@ -249,7 +248,7 @@ def _write(self, data: Optional[Dict] = None, summary: Optional[str] = None, log return json_result['entity'] - def delete(self, login: Optional[_Login] = None, allow_anonymous: bool = False, is_bot: Optional[bool] = None, **kwargs: Any): + def delete(self, login: _Login | None = None, allow_anonymous: bool = False, is_bot: bool | None = None, **kwargs: Any): """ Delete the current entity. Use the pageid first if available and fallback to the page title. @@ -276,7 +275,7 @@ def delete(self, login: Optional[_Login] = None, allow_anonymous: bool = False, return delete_page(title=None, pageid=self.pageid, login=login, allow_anonymous=allow_anonymous, is_bot=is_bot, **kwargs) - def write_required(self, base_filter: Optional[List[BaseDataType | List[BaseDataType]]] = None, action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL, + def write_required(self, base_filter: list[BaseDataType | list[BaseDataType]] | None = None, action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL, **kwargs: Any) -> bool: fastrun_container = wbi_fastrun.get_fastrun_container(base_filter=base_filter, **kwargs) @@ -292,7 +291,7 @@ def write_required(self, base_filter: Optional[List[BaseDataType | List[BaseData return fastrun_container.write_required(data=claims_to_check, cqid=self.id, action_if_exists=action_if_exists) - def get_entity_url(self, wikibase_url: Optional[str] = None) -> str: + def get_entity_url(self, wikibase_url: str | None = None) -> str: from wikibaseintegrator.wbi_config import config wikibase_url = wikibase_url or str(config['WIKIBASE_URL']) if wikibase_url and self.id: diff --git a/wikibaseintegrator/entities/item.py b/wikibaseintegrator/entities/item.py index e16d46c7..94f21589 100644 --- a/wikibaseintegrator/entities/item.py +++ b/wikibaseintegrator/entities/item.py @@ -1,7 +1,7 @@ from __future__ import annotations import re -from typing import Any, Dict, Optional, Union +from typing import Any from wikibaseintegrator.entities.baseentity import BaseEntity from wikibaseintegrator.models import LanguageValues @@ -14,7 +14,7 @@ class ItemEntity(BaseEntity): ETYPE = 'item' - def __init__(self, labels: Optional[Labels] = None, descriptions: Optional[Descriptions] = None, aliases: Optional[Aliases] = None, sitelinks: Optional[Sitelinks] = None, **kwargs: Any) -> None: + def __init__(self, labels: Labels | None = None, descriptions: Descriptions | None = None, aliases: Aliases | None = None, sitelinks: Sitelinks | None = None, **kwargs: Any) -> None: """ :param api: @@ -35,7 +35,7 @@ def __init__(self, labels: Optional[Labels] = None, descriptions: Optional[Descr self.sitelinks = sitelinks or Sitelinks() @BaseEntity.id.setter # type: ignore - def id(self, value: Union[None, str, int]): + def id(self, value: None | str | int): if isinstance(value, str): pattern = re.compile(r'^(?:[a-zA-Z]+:)?Q?([0-9]+)$') matches = pattern.match(value) @@ -96,7 +96,7 @@ def sitelinks(self, sitelinks: Sitelinks): def new(self, **kwargs: Any) -> ItemEntity: return ItemEntity(api=self.api, **kwargs) - def get(self, entity_id: Optional[Union[str, int]] = None, **kwargs: Any) -> ItemEntity: + def get(self, entity_id: str | int | None = None, **kwargs: Any) -> ItemEntity: """ Request the MediaWiki API to get data for the entity specified in argument. @@ -126,7 +126,7 @@ def get(self, entity_id: Optional[Union[str, int]] = None, **kwargs: Any) -> Ite json_data = super()._get(entity_id=entity_id, **kwargs) return ItemEntity(api=self.api).from_json(json_data=json_data['entities'][entity_id]) - def get_json(self) -> Dict[str, Union[str, Dict]]: + def get_json(self) -> dict[str, str | dict]: """ To get the dict equivalent of the JSON representation of the Item. @@ -139,7 +139,7 @@ def get_json(self) -> Dict[str, Union[str, Dict]]: **super().get_json() } - def from_json(self, json_data: Dict[str, Any]) -> ItemEntity: + def from_json(self, json_data: dict[str, Any]) -> ItemEntity: super().from_json(json_data=json_data) self.labels = Labels().from_json(json_data['labels']) diff --git a/wikibaseintegrator/entities/lexeme.py b/wikibaseintegrator/entities/lexeme.py index c2c295f0..1c18e1e4 100644 --- a/wikibaseintegrator/entities/lexeme.py +++ b/wikibaseintegrator/entities/lexeme.py @@ -1,7 +1,7 @@ from __future__ import annotations import re -from typing import Any, Dict, Optional, Union +from typing import Any from wikibaseintegrator.entities.baseentity import BaseEntity from wikibaseintegrator.models.forms import Forms @@ -13,18 +13,18 @@ class LexemeEntity(BaseEntity): ETYPE = 'lexeme' - def __init__(self, lemmas: Optional[Lemmas] = None, lexical_category: Optional[str] = None, language: Optional[str] = None, forms: Optional[Forms] = None, - senses: Optional[Senses] = None, **kwargs: Any): + def __init__(self, lemmas: Lemmas | None = None, lexical_category: str | None = None, language: str | None = None, forms: Forms | None = None, senses: Senses | None = None, + **kwargs: Any): super().__init__(**kwargs) self.lemmas: Lemmas = lemmas or Lemmas() - self.lexical_category: Optional[str] = lexical_category + self.lexical_category: str | None = lexical_category self.language: str = str(language or config['DEFAULT_LEXEME_LANGUAGE']) self.forms: Forms = forms or Forms() self.senses: Senses = senses or Senses() @BaseEntity.id.setter # type: ignore - def id(self, value: Union[None, str, int]): + def id(self, value: None | str | int): if isinstance(value, str): pattern = re.compile(r'^(?:[a-zA-Z]+:)?L?([0-9]+)$') matches = pattern.match(value) @@ -53,11 +53,11 @@ def lemmas(self, lemmas: Lemmas): self.__lemmas = lemmas @property - def lexical_category(self) -> Optional[str]: + def lexical_category(self) -> str | None: return self.__lexical_category @lexical_category.setter - def lexical_category(self, lexical_category: Optional[str]): + def lexical_category(self, lexical_category: str | None): self.__lexical_category = lexical_category @property @@ -106,7 +106,7 @@ def senses(self, senses: Senses): def new(self, **kwargs: Any) -> LexemeEntity: return LexemeEntity(api=self.api, **kwargs) - def get(self, entity_id: Union[str, int], **kwargs: Any) -> LexemeEntity: + def get(self, entity_id: str | int, **kwargs: Any) -> LexemeEntity: if isinstance(entity_id, str): pattern = re.compile(r'^(?:[a-zA-Z]+:)?L?([0-9]+)$') matches = pattern.match(entity_id) @@ -123,8 +123,8 @@ def get(self, entity_id: Union[str, int], **kwargs: Any) -> LexemeEntity: json_data = super()._get(entity_id=entity_id, **kwargs) return LexemeEntity(api=self.api).from_json(json_data=json_data['entities'][entity_id]) - def get_json(self) -> Dict[str, Union[str, Dict]]: - json_data: Dict = { + def get_json(self) -> dict[str, str | dict]: + json_data: dict = { 'lemmas': self.lemmas.get_json(), 'language': self.language, 'forms': self.forms.get_json(), @@ -137,7 +137,7 @@ def get_json(self) -> Dict[str, Union[str, Dict]]: return json_data - def from_json(self, json_data: Dict[str, Any]) -> LexemeEntity: + def from_json(self, json_data: dict[str, Any]) -> LexemeEntity: super().from_json(json_data=json_data) self.lemmas = Lemmas().from_json(json_data['lemmas']) diff --git a/wikibaseintegrator/entities/mediainfo.py b/wikibaseintegrator/entities/mediainfo.py index 73cd4d7b..dd27137a 100644 --- a/wikibaseintegrator/entities/mediainfo.py +++ b/wikibaseintegrator/entities/mediainfo.py @@ -1,7 +1,7 @@ from __future__ import annotations import re -from typing import Any, Dict, List, Optional, Union +from typing import Any from wikibaseintegrator.entities.baseentity import BaseEntity from wikibaseintegrator.models import Claims, LanguageValues @@ -14,7 +14,7 @@ class MediaInfoEntity(BaseEntity): ETYPE = 'mediainfo' - def __init__(self, labels: Optional[Labels] = None, descriptions: Optional[Descriptions] = None, aliases: Optional[Aliases] = None, **kwargs: Any) -> None: + def __init__(self, labels: Labels | None = None, descriptions: Descriptions | None = None, aliases: Aliases | None = None, **kwargs: Any) -> None: """ :param api: @@ -32,7 +32,7 @@ def __init__(self, labels: Optional[Labels] = None, descriptions: Optional[Descr self.aliases = aliases or Aliases() @BaseEntity.id.setter # type: ignore - def id(self, value: Union[None, str, int]): + def id(self, value: None | str | int): if isinstance(value, str): pattern = re.compile(r'^M?([0-9]+)$') matches = pattern.match(value) @@ -83,7 +83,7 @@ def aliases(self, aliases: Aliases): def new(self, **kwargs: Any) -> MediaInfoEntity: return MediaInfoEntity(api=self.api, **kwargs) - def get(self, entity_id: Union[str, int], **kwargs: Any) -> MediaInfoEntity: + def get(self, entity_id: str | int, **kwargs: Any) -> MediaInfoEntity: if isinstance(entity_id, str): pattern = re.compile(r'^M?([0-9]+)$') matches = pattern.match(entity_id) @@ -100,7 +100,7 @@ def get(self, entity_id: Union[str, int], **kwargs: Any) -> MediaInfoEntity: json_data = super()._get(entity_id=entity_id, **kwargs) return MediaInfoEntity(api=self.api).from_json(json_data=json_data['entities'][entity_id]) - def get_by_title(self, titles: Union[List[str], str], sites: str = 'commonswiki', **kwargs: Any) -> MediaInfoEntity: + def get_by_title(self, titles: list[str] | str, sites: str = 'commonswiki', **kwargs: Any) -> MediaInfoEntity: if isinstance(titles, list): titles = '|'.join(titles) @@ -120,7 +120,7 @@ def get_by_title(self, titles: Union[List[str], str], sites: str = 'commonswiki' return MediaInfoEntity(api=self.api).from_json(json_data=json_data['entities'][list(json_data['entities'].keys())[0]]) - def get_json(self) -> Dict[str, Union[str, Dict]]: + def get_json(self) -> dict[str, str | dict]: return { 'labels': self.labels.get_json(), 'descriptions': self.descriptions.get_json(), @@ -136,7 +136,7 @@ def get_json(self) -> Dict[str, Union[str, Dict]]: # if 'mainsnak' in statement and 'datatype' in statement['mainsnak']: # del statement['mainsnak']['datatype'] - def from_json(self, json_data: Dict[str, Any]) -> MediaInfoEntity: + def from_json(self, json_data: dict[str, Any]) -> MediaInfoEntity: super().from_json(json_data=json_data) self.labels = Labels().from_json(json_data['labels']) diff --git a/wikibaseintegrator/entities/property.py b/wikibaseintegrator/entities/property.py index 6bd99529..d8ed2107 100644 --- a/wikibaseintegrator/entities/property.py +++ b/wikibaseintegrator/entities/property.py @@ -1,7 +1,7 @@ from __future__ import annotations import re -from typing import Any, Dict, Optional, Union +from typing import Any from wikibaseintegrator.entities.baseentity import BaseEntity from wikibaseintegrator.models.aliases import Aliases @@ -13,7 +13,7 @@ class PropertyEntity(BaseEntity): ETYPE = 'property' - def __init__(self, datatype: Union[str, WikibaseDatatype, None] = None, labels: Optional[Labels] = None, descriptions: Optional[Descriptions] = None, aliases: Optional[Aliases] = None, **kwargs: Any): + def __init__(self, datatype: str | WikibaseDatatype | None = None, labels: Labels | None = None, descriptions: Descriptions | None = None, aliases: Aliases | None = None, **kwargs: Any): super().__init__(**kwargs) # Property specific @@ -25,7 +25,7 @@ def __init__(self, datatype: Union[str, WikibaseDatatype, None] = None, labels: self.aliases = aliases or Aliases() @BaseEntity.id.setter # type: ignore - def id(self, value: Union[None, str, int]): + def id(self, value: None | str | int): if isinstance(value, str): pattern = re.compile(r'^(?:[a-zA-Z]+:)?P?([0-9]+)$') matches = pattern.match(value) @@ -44,13 +44,13 @@ def id(self, value: Union[None, str, int]): BaseEntity.id.fset(self, value) # type: ignore @property - def datatype(self) -> Union[str, WikibaseDatatype, None]: + def datatype(self) -> str | WikibaseDatatype | None: return self.__datatype @datatype.setter - def datatype(self, value: Union[str, WikibaseDatatype, None]): + def datatype(self, value: str | WikibaseDatatype | None): if isinstance(value, str): - self.__datatype: Union[str, WikibaseDatatype, None] = WikibaseDatatype(value) + self.__datatype: str | WikibaseDatatype | None = WikibaseDatatype(value) else: self.__datatype = value @@ -87,7 +87,7 @@ def aliases(self, aliases: Aliases): def new(self, **kwargs: Any) -> PropertyEntity: return PropertyEntity(api=self.api, **kwargs) - def get(self, entity_id: Union[str, int], **kwargs: Any) -> PropertyEntity: + def get(self, entity_id: str | int, **kwargs: Any) -> PropertyEntity: if isinstance(entity_id, str): pattern = re.compile(r'^(?:[a-zA-Z]+:)?P?([0-9]+)$') matches = pattern.match(entity_id) @@ -104,7 +104,7 @@ def get(self, entity_id: Union[str, int], **kwargs: Any) -> PropertyEntity: json_data = super()._get(entity_id=entity_id, **kwargs) return PropertyEntity(api=self.api).from_json(json_data=json_data['entities'][entity_id]) - def get_json(self) -> Dict[str, Union[str, Any]]: + def get_json(self) -> dict[str, str | Any]: json = { 'labels': self.labels.get_json(), 'descriptions': self.descriptions.get_json(), @@ -117,7 +117,7 @@ def get_json(self) -> Dict[str, Union[str, Any]]: return json - def from_json(self, json_data: Dict[str, Any]) -> PropertyEntity: + def from_json(self, json_data: dict[str, Any]) -> PropertyEntity: super().from_json(json_data=json_data) if 'datatype' in json_data: # TODO: 1.35 compatibility diff --git a/wikibaseintegrator/models/aliases.py b/wikibaseintegrator/models/aliases.py index 13de677f..8bc60d63 100644 --- a/wikibaseintegrator/models/aliases.py +++ b/wikibaseintegrator/models/aliases.py @@ -1,7 +1,5 @@ from __future__ import annotations -from typing import Dict, List, Optional, Union - from wikibaseintegrator.models.basemodel import BaseModel from wikibaseintegrator.models.language_values import LanguageValue from wikibaseintegrator.wbi_config import config @@ -9,21 +7,21 @@ class Aliases(BaseModel): - def __init__(self, language: Optional[str] = None, value: Optional[str] = None): - self.aliases: Dict[str, List[Alias]] = {} + def __init__(self, language: str | None = None, value: str | None = None): + self.aliases: dict[str, list[Alias]] = {} if language is not None: self.set(language=language, values=value) @property - def aliases(self) -> Dict[str, List[Alias]]: + def aliases(self) -> dict[str, list[Alias]]: return self.__aliases @aliases.setter - def aliases(self, value: Dict[str, List[Alias]]): + def aliases(self, value: dict[str, list[Alias]]): self.__aliases = value - def get(self, language: Optional[str] = None) -> Optional[List[Alias]]: + def get(self, language: str | None = None) -> list[Alias] | None: if language is None: # TODO: Don't return a list of list, just a list return [item for sublist in self.aliases.values() for item in sublist] @@ -33,7 +31,7 @@ def get(self, language: Optional[str] = None) -> Optional[List[Alias]]: return None - def set(self, language: Optional[str] = None, values: Optional[Union[str, List]] = None, action_if_exists: ActionIfExists = ActionIfExists.APPEND_OR_REPLACE) -> Aliases: + def set(self, language: str | None = None, values: str | list | None = None, action_if_exists: ActionIfExists = ActionIfExists.APPEND_OR_REPLACE) -> Aliases: language = str(language or config['DEFAULT_LANGUAGE']) assert action_if_exists in ActionIfExists @@ -72,8 +70,8 @@ def set(self, language: Optional[str] = None, values: Optional[Union[str, List]] return self - def get_json(self) -> Dict[str, List]: - json_data: Dict[str, List] = {} + def get_json(self) -> dict[str, list]: + json_data: dict[str, list] = {} for language, aliases in self.aliases.items(): if language not in json_data: json_data[language] = [] @@ -81,7 +79,7 @@ def get_json(self) -> Dict[str, List]: json_data[language].append(alias.get_json()) return json_data - def from_json(self, json_data: Dict[str, List]) -> Aliases: + def from_json(self, json_data: dict[str, list]) -> Aliases: for language in json_data: for alias in json_data[language]: self.set(alias['language'], alias['value']) diff --git a/wikibaseintegrator/models/claims.py b/wikibaseintegrator/models/claims.py index f27cb4c3..37791df5 100644 --- a/wikibaseintegrator/models/claims.py +++ b/wikibaseintegrator/models/claims.py @@ -2,7 +2,7 @@ import copy from abc import abstractmethod -from typing import Any, Callable, Dict, List, Optional, Union +from typing import Any, Callable from wikibaseintegrator.models.basemodel import BaseModel from wikibaseintegrator.models.qualifiers import Qualifiers @@ -13,20 +13,20 @@ class Claims(BaseModel): def __init__(self) -> None: - self.claims: Dict[str, List[Claim]] = {} + self.claims: dict[str, list[Claim]] = {} @property - def claims(self) -> Dict[str, List[Claim]]: + def claims(self) -> dict[str, list[Claim]]: return self.__claims @claims.setter - def claims(self, claims: Dict[str, List[Claim]]): + def claims(self, claims: dict[str, list[Claim]]): self.__claims = claims - def get(self, property: str) -> List[Claim]: + def get(self, property: str) -> list[Claim]: return self.claims[property] - def remove(self, property: Optional[str] = None) -> None: + def remove(self, property: str | None = None) -> None: if property in self.claims: for prop in self.claims[property]: if prop.id: @@ -36,7 +36,7 @@ def remove(self, property: Optional[str] = None) -> None: if len(self.claims[property]) == 0: del self.claims[property] - def add(self, claims: Union[Claims, List[Claim], Claim], action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL) -> Claims: + def add(self, claims: Claims | list[Claim] | Claim, action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL) -> Claims: """ :param claims: A Claim, list of Claim or just a Claims object to add to this Claims object. @@ -94,7 +94,7 @@ def add(self, claims: Union[Claims, List[Claim], Claim], action_if_exists: Actio return self - def from_json(self, json_data: Dict[str, Any]) -> Claims: + def from_json(self, json_data: dict[str, Any]) -> Claims: for property in json_data: for claim in json_data[property]: from wikibaseintegrator.datatypes import BaseDataType @@ -106,8 +106,8 @@ def from_json(self, json_data: Dict[str, Any]) -> Claims: return self - def get_json(self) -> Dict[str, List]: - json_data: Dict[str, List] = {} + def get_json(self) -> dict[str, list]: + json_data: dict[str, list] = {} for property, claims in self.claims.items(): if property not in json_data: json_data[property] = [] @@ -131,8 +131,8 @@ def __len__(self): class Claim(BaseModel): DTYPE = 'claim' - def __init__(self, qualifiers: Optional[Qualifiers] = None, rank: Optional[WikibaseRank] = None, - references: Optional[Union[References, List[Union[Claim, List[Claim]]]]] = None, snaktype: WikibaseSnakType = WikibaseSnakType.KNOWN_VALUE) -> None: + def __init__(self, qualifiers: Qualifiers | None = None, rank: WikibaseRank | None = None, references: References | list[Claim | list[Claim]] | None = None, + snaktype: WikibaseSnakType = WikibaseSnakType.KNOWN_VALUE) -> None: """ :param qualifiers: @@ -180,11 +180,11 @@ def mainsnak(self, value: Snak): self.__mainsnak = value @property - def type(self) -> Union[str, Dict]: + def type(self) -> str | dict: return self.__type @type.setter - def type(self, value: Union[str, Dict]): + def type(self, value: str | dict): self.__type = value @property @@ -197,19 +197,19 @@ def qualifiers(self, value: Qualifiers) -> None: self.__qualifiers: Qualifiers = Qualifiers().set(value) if isinstance(value, list) else value @property - def qualifiers_order(self) -> List[str]: + def qualifiers_order(self) -> list[str]: return self.__qualifiers_order @qualifiers_order.setter - def qualifiers_order(self, value: List[str]): + def qualifiers_order(self, value: list[str]): self.__qualifiers_order = value @property - def id(self) -> Optional[str]: + def id(self) -> str | None: return self.__id @id.setter - def id(self, value: Optional[str]): + def id(self, value: str | None): self.__id = value @property @@ -247,7 +247,7 @@ def update(self, claim: Claim) -> None: self.rank = claim.rank self.references = claim.references - def from_json(self, json_data: Dict[str, Any]) -> Claim: + def from_json(self, json_data: dict[str, Any]) -> Claim: """ :param json_data: a JSON representation of a Claim @@ -265,8 +265,8 @@ def from_json(self, json_data: Dict[str, Any]) -> Claim: return self - def get_json(self) -> Dict[str, Any]: - json_data: Dict[str, Union[str, List[Dict], List[str], Dict[str, str], Dict[str, List], None]] = { + def get_json(self) -> dict[str, Any]: + json_data: dict[str, str | list[dict] | list[str] | dict[str, str] | dict[str, list] | None] = { 'mainsnak': self.mainsnak.get_json(), 'type': self.type, 'id': self.id, @@ -335,7 +335,7 @@ def __eq__(self, other): raise super().__eq__(other) - def equals(self, that: Claim, include_ref: bool = False, fref: Optional[Callable] = None) -> bool: + def equals(self, that: Claim, include_ref: bool = False, fref: Callable | None = None) -> bool: """ Tests for equality of two statements. If comparing references, the order of the arguments matters!!! diff --git a/wikibaseintegrator/models/descriptions.py b/wikibaseintegrator/models/descriptions.py index 872e730c..efadc929 100644 --- a/wikibaseintegrator/models/descriptions.py +++ b/wikibaseintegrator/models/descriptions.py @@ -1,12 +1,10 @@ from __future__ import annotations -from typing import Dict - from wikibaseintegrator.models.language_values import LanguageValue, LanguageValues class Descriptions(LanguageValues): - def from_json(self, json_data: Dict[str, Dict]) -> Descriptions: + def from_json(self, json_data: dict[str, dict]) -> Descriptions: """ Create a new Descriptions object from a JSON/dict object. diff --git a/wikibaseintegrator/models/forms.py b/wikibaseintegrator/models/forms.py index 6a613cf8..bd8b5d99 100644 --- a/wikibaseintegrator/models/forms.py +++ b/wikibaseintegrator/models/forms.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any, Dict, List, Optional, Union +from typing import Any from wikibaseintegrator.models.basemodel import BaseModel from wikibaseintegrator.models.claims import Claims @@ -9,10 +9,10 @@ class Forms(BaseModel): def __init__(self) -> None: - self.forms: Dict[str, Form] = {} + self.forms: dict[str, Form] = {} @property - def forms(self) -> Dict: + def forms(self) -> dict: return self.__forms @forms.setter @@ -27,14 +27,14 @@ def add(self, form: Form) -> Forms: return self - def from_json(self, json_data: List[Dict]) -> Forms: + def from_json(self, json_data: list[dict]) -> Forms: for form in json_data: self.add(form=Form().from_json(form)) return self - def get_json(self) -> List[Dict]: - json_data: List[Dict] = [] + def get_json(self) -> list[dict]: + json_data: list[dict] = [] for _, form in self.forms.items(): json_data.append(form.get_json()) @@ -45,7 +45,7 @@ def __len__(self): class Form(BaseModel): - def __init__(self, form_id: Optional[str] = None, representations: Optional[Representations] = None, grammatical_features: Optional[Union[str, int, List[str]]] = None, claims: Optional[Claims] = None): + def __init__(self, form_id: str | None = None, representations: Representations | None = None, grammatical_features: str | int | list[str] | None = None, claims: Claims | None = None): self.id = form_id self.representations: Representations = representations or LanguageValues() self.grammatical_features = grammatical_features or [] @@ -72,7 +72,7 @@ def grammatical_features(self): return self.__grammatical_features @grammatical_features.setter - def grammatical_features(self, value: Union[str, int, List[str]]): + def grammatical_features(self, value: str | int | list[str]): if not hasattr(self, '__grammatical_features') or value is None: self.__grammatical_features = [] @@ -93,7 +93,7 @@ def claims(self): def claims(self, value): self.__claims = value - def from_json(self, json_data: Dict[str, Any]) -> Form: + def from_json(self, json_data: dict[str, Any]) -> Form: self.id = json_data['id'] self.representations = Representations().from_json(json_data['representations']) self.grammatical_features = json_data['grammaticalFeatures'] @@ -101,8 +101,8 @@ def from_json(self, json_data: Dict[str, Any]) -> Form: return self - def get_json(self) -> Dict[str, Union[str, Dict, List]]: - json_data: Dict[str, Union[str, Dict, List]] = { + def get_json(self) -> dict[str, str | dict | list]: + json_data: dict[str, str | dict | list] = { 'id': self.id, 'representations': self.representations.get_json(), 'grammaticalFeatures': self.grammatical_features, diff --git a/wikibaseintegrator/models/labels.py b/wikibaseintegrator/models/labels.py index 6c186446..6a4af0d0 100644 --- a/wikibaseintegrator/models/labels.py +++ b/wikibaseintegrator/models/labels.py @@ -1,12 +1,10 @@ from __future__ import annotations -from typing import Dict - from wikibaseintegrator.models.language_values import LanguageValue, LanguageValues class Labels(LanguageValues): - def from_json(self, json_data: Dict[str, Dict]) -> Labels: + def from_json(self, json_data: dict[str, dict]) -> Labels: """ Create a new Labels object from a JSON/dict object. diff --git a/wikibaseintegrator/models/language_values.py b/wikibaseintegrator/models/language_values.py index 1ead1799..28935e94 100644 --- a/wikibaseintegrator/models/language_values.py +++ b/wikibaseintegrator/models/language_values.py @@ -1,7 +1,5 @@ from __future__ import annotations -from typing import Dict, Optional - from wikibaseintegrator.models.basemodel import BaseModel from wikibaseintegrator.wbi_config import config from wikibaseintegrator.wbi_enums import ActionIfExists @@ -9,17 +7,17 @@ class LanguageValues(BaseModel): def __init__(self) -> None: - self.values: Dict[str, LanguageValue] = {} + self.values: dict[str, LanguageValue] = {} @property - def values(self) -> Dict[str, LanguageValue]: + def values(self) -> dict[str, LanguageValue]: """ A dict of LanguageValue with the language as key. """ return self.__values @values.setter - def values(self, value: Dict[str, LanguageValue]): + def values(self, value: dict[str, LanguageValue]): self.__values = value def add(self, language_value: LanguageValue) -> LanguageValues: @@ -36,7 +34,7 @@ def add(self, language_value: LanguageValue) -> LanguageValues: return self - def get(self, language: Optional[str] = None) -> Optional[LanguageValue]: + def get(self, language: str | None = None) -> LanguageValue | None: """ Get a LanguageValue object with the specified language. Use the default language in wbi_config if none specified. @@ -49,7 +47,7 @@ def get(self, language: Optional[str] = None) -> Optional[LanguageValue]: return None - def set(self, language: Optional[str] = None, value: Optional[str] = None, action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL) -> Optional[LanguageValue]: + def set(self, language: str | None = None, value: str | None = None, action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL) -> LanguageValue | None: """ Create or update the specified language with the valued passed in arguments. @@ -74,7 +72,7 @@ def set(self, language: Optional[str] = None, value: Optional[str] = None, actio return self.get(language=language) - def from_json(self, json_data: Dict[str, Dict]) -> LanguageValues: + def from_json(self, json_data: dict[str, dict]) -> LanguageValues: """ Create a new LanguageValues object from a JSON/dict object. @@ -86,13 +84,13 @@ def from_json(self, json_data: Dict[str, Dict]) -> LanguageValues: return self - def get_json(self) -> Dict[str, Dict]: + def get_json(self) -> dict[str, dict]: """ Get a formatted dict who respect the Wikibase format. :return: A dict using Wikibase format. """ - json_data: Dict[str, Dict] = {} + json_data: dict[str, dict] = {} for language, language_value in self.values.items(): json_data[language] = language_value.get_json() @@ -109,7 +107,7 @@ def __len__(self): class LanguageValue(BaseModel): - def __init__(self, language: str, value: Optional[str] = None): + def __init__(self, language: str, value: str | None = None): self.language = language self.value = value self.removed = False @@ -119,7 +117,7 @@ def language(self) -> str: return self.__language @language.setter - def language(self, value: Optional[str]): + def language(self, value: str | None): if value is None: raise ValueError("language can't be None") @@ -132,7 +130,7 @@ def language(self, value: Optional[str]): self.__language = value @property - def value(self) -> Optional[str]: + def value(self) -> str | None: """ The value of the LanguageValue instance. :return: A string with the value of the LanguageValue instance. @@ -140,7 +138,7 @@ def value(self) -> Optional[str]: return self.__value @value.setter - def value(self, value: Optional[str]): + def value(self, value: str | None): self.__value = value @property @@ -156,13 +154,13 @@ def remove(self) -> LanguageValue: return self - def from_json(self, json_data: Dict[str, str]) -> LanguageValue: + def from_json(self, json_data: dict[str, str]) -> LanguageValue: self.language = json_data['language'] self.value = json_data['value'] return self - def get_json(self) -> Dict[str, Optional[str]]: + def get_json(self) -> dict[str, str | None]: json_data = { 'language': self.language, 'value': self.value diff --git a/wikibaseintegrator/models/lemmas.py b/wikibaseintegrator/models/lemmas.py index 01a9b268..e795e050 100644 --- a/wikibaseintegrator/models/lemmas.py +++ b/wikibaseintegrator/models/lemmas.py @@ -1,12 +1,10 @@ from __future__ import annotations -from typing import Dict - from wikibaseintegrator.models.language_values import LanguageValue, LanguageValues class Lemmas(LanguageValues): - def from_json(self, json_data: Dict[str, Dict]) -> Lemmas: + def from_json(self, json_data: dict[str, dict]) -> Lemmas: """ Create a new Lemmas object from a JSON/dict object. diff --git a/wikibaseintegrator/models/qualifiers.py b/wikibaseintegrator/models/qualifiers.py index 53466c31..aa4fb1f8 100644 --- a/wikibaseintegrator/models/qualifiers.py +++ b/wikibaseintegrator/models/qualifiers.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Dict, List, Optional, Union +from typing import TYPE_CHECKING from wikibaseintegrator.models.basemodel import BaseModel from wikibaseintegrator.models.snaks import Snak @@ -12,7 +12,7 @@ class Qualifiers(BaseModel): def __init__(self) -> None: - self.qualifiers: Dict[str, List[Snak]] = {} + self.qualifiers: dict[str, list[Snak]] = {} @property def qualifiers(self): @@ -23,7 +23,7 @@ def qualifiers(self, value): assert isinstance(value, dict) self.__qualifiers = value - def set(self, qualifiers: Union[Qualifiers, List[Union[Snak, Claim]], None]) -> Qualifiers: + def set(self, qualifiers: Qualifiers | list[Snak | Claim] | None) -> Qualifiers: if isinstance(qualifiers, list) or isinstance(qualifiers, Qualifiers): for qualifier in qualifiers: self.add(qualifier) @@ -34,7 +34,7 @@ def set(self, qualifiers: Union[Qualifiers, List[Union[Snak, Claim]], None]) -> return self - def get(self, property: Union[str, int]) -> List[Snak]: + def get(self, property: str | int) -> list[Snak]: if isinstance(property, int): property = 'P' + str(property) @@ -44,7 +44,7 @@ def get(self, property: Union[str, int]) -> List[Snak]: return [] # TODO: implement action_if_exists - def add(self, qualifier: Union[Snak, Claim], action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL) -> Qualifiers: + def add(self, qualifier: Snak | Claim, action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL) -> Qualifiers: from wikibaseintegrator.models.claims import Claim if isinstance(qualifier, Claim): qualifier = Snak().from_json(qualifier.get_json()['mainsnak']) @@ -61,7 +61,7 @@ def add(self, qualifier: Union[Snak, Claim], action_if_exists: ActionIfExists = return self - def remove(self, qualifier: Union[Snak, Claim]) -> Qualifiers: + def remove(self, qualifier: Snak | Claim) -> Qualifiers: from wikibaseintegrator.models.claims import Claim if isinstance(qualifier, Claim): qualifier = Snak().from_json(qualifier.get_json()['mainsnak']) @@ -77,7 +77,7 @@ def remove(self, qualifier: Union[Snak, Claim]) -> Qualifiers: return self - def clear(self, property: Optional[Union[str, int]] = None) -> Qualifiers: + def clear(self, property: str | int | None = None) -> Qualifiers: if isinstance(property, int): property = 'P' + str(property) @@ -87,14 +87,14 @@ def clear(self, property: Optional[Union[str, int]] = None) -> Qualifiers: del self.qualifiers[property] return self - def from_json(self, json_data: Dict[str, List]) -> Qualifiers: + def from_json(self, json_data: dict[str, list]) -> Qualifiers: for property in json_data: for snak in json_data[property]: self.add(qualifier=Snak().from_json(snak)) return self - def get_json(self) -> Dict[str, List]: - json_data: Dict[str, List] = {} + def get_json(self) -> dict[str, list]: + json_data: dict[str, list] = {} for property in self.qualifiers: if property not in json_data: json_data[property] = [] diff --git a/wikibaseintegrator/models/references.py b/wikibaseintegrator/models/references.py index bdb90ca8..8ab0f713 100644 --- a/wikibaseintegrator/models/references.py +++ b/wikibaseintegrator/models/references.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Any from wikibaseintegrator.models.basemodel import BaseModel from wikibaseintegrator.models.snaks import Snak, Snaks @@ -12,24 +12,24 @@ class References(BaseModel): def __init__(self) -> None: - self.references: List[Reference] = [] + self.references: list[Reference] = [] @property - def references(self) -> List[Reference]: + def references(self) -> list[Reference]: return self.__references @references.setter - def references(self, value: List[Reference]): + def references(self, value: list[Reference]): self.__references = value - def get(self, hash: Optional[str] = None) -> Optional[Reference]: + def get(self, hash: str | None = None) -> Reference | None: for reference in self.references: if reference.hash == hash: return reference return None # TODO: implement action_if_exists - def add(self, reference: Optional[Union[Reference, Claim]] = None, action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL) -> References: + def add(self, reference: Reference | Claim | None = None, action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL) -> References: from wikibaseintegrator.models.claims import Claim if isinstance(reference, Claim): reference = Reference(snaks=Snaks().add(Snak().from_json(reference.get_json()['mainsnak']))) @@ -42,19 +42,19 @@ def add(self, reference: Optional[Union[Reference, Claim]] = None, action_if_exi return self - def from_json(self, json_data: List[Dict]) -> References: + def from_json(self, json_data: list[dict]) -> References: for reference in json_data: self.add(reference=Reference().from_json(reference)) return self - def get_json(self) -> List[Dict]: - json_data: List[Dict] = [] + def get_json(self) -> list[dict]: + json_data: list[dict] = [] for reference in self.references: json_data.append(reference.get_json()) return json_data - def remove(self, reference_to_remove: Union[Claim, Reference]) -> bool: + def remove(self, reference_to_remove: Claim | Reference) -> bool: from wikibaseintegrator.models.claims import Claim if isinstance(reference_to_remove, Claim): reference_to_remove = Reference(snaks=Snaks().add(Snak().from_json(reference_to_remove.get_json()['mainsnak']))) @@ -80,7 +80,7 @@ def __len__(self): class Reference(BaseModel): - def __init__(self, snaks: Optional[Snaks] = None, snaks_order: Optional[List] = None): + def __init__(self, snaks: Snaks | None = None, snaks_order: list | None = None): self.hash = None self.snaks = snaks or Snaks() self.snaks_order = snaks_order or [] @@ -110,7 +110,7 @@ def snaks_order(self, value): self.__snaks_order = value # TODO: implement action_if_exists - def add(self, snak: Optional[Union[Snak, Claim]] = None, action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL) -> Reference: + def add(self, snak: Snak | Claim | None = None, action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL) -> Reference: from wikibaseintegrator.models.claims import Claim if isinstance(snak, Claim): snak = Snak().from_json(snak.get_json()['mainsnak']) @@ -122,15 +122,15 @@ def add(self, snak: Optional[Union[Snak, Claim]] = None, action_if_exists: Actio return self - def from_json(self, json_data: Dict[str, Any]) -> Reference: + def from_json(self, json_data: dict[str, Any]) -> Reference: self.hash = json_data['hash'] self.snaks = Snaks().from_json(json_data['snaks']) self.snaks_order = json_data['snaks-order'] return self - def get_json(self) -> Dict[str, Union[Dict, List]]: - json_data: Dict[str, Union[Dict, List]] = { + def get_json(self) -> dict[str, dict | list]: + json_data: dict[str, dict | list] = { 'snaks': self.snaks.get_json(), 'snaks-order': self.snaks_order } diff --git a/wikibaseintegrator/models/senses.py b/wikibaseintegrator/models/senses.py index 1dfea02d..4778fa78 100644 --- a/wikibaseintegrator/models/senses.py +++ b/wikibaseintegrator/models/senses.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any, Dict, List, Optional, Union +from typing import Any from wikibaseintegrator.models.basemodel import BaseModel from wikibaseintegrator.models.claims import Claims @@ -10,9 +10,9 @@ class Senses(BaseModel): def __init__(self) -> None: - self.senses: List[Sense] = [] + self.senses: list[Sense] = [] - def get(self, id: str) -> Optional[Sense]: + def get(self, id: str) -> Sense | None: for sense in self.senses: if sense.id == id: return sense @@ -24,14 +24,14 @@ def add(self, sense: Sense, action_if_exists: ActionIfExists = ActionIfExists.RE return self - def from_json(self, json_data: List[Dict]) -> Senses: + def from_json(self, json_data: list[dict]) -> Senses: for sense in json_data: self.add(sense=Sense().from_json(sense)) return self - def get_json(self) -> List[Dict]: - json_data: List[Dict] = [] + def get_json(self) -> list[dict]: + json_data: list[dict] = [] for sense in self.senses: json_data.append(sense.get_json()) @@ -42,21 +42,21 @@ def __len__(self): class Sense(BaseModel): - def __init__(self, sense_id: Optional[str] = None, glosses: Optional[Glosses] = None, claims: Optional[Claims] = None): + def __init__(self, sense_id: str | None = None, glosses: Glosses | None = None, claims: Claims | None = None): self.id = sense_id self.glosses: LanguageValues = glosses or Glosses() self.claims = claims or Claims() self.removed = False - def from_json(self, json_data: Dict[str, Any]) -> Sense: + def from_json(self, json_data: dict[str, Any]) -> Sense: self.id = json_data['id'] self.glosses = Glosses().from_json(json_data['glosses']) self.claims = Claims().from_json(json_data['claims']) return self - def get_json(self) -> Dict[str, Union[str, Dict]]: - json_data: Dict[str, Union[str, Dict]] = { + def get_json(self) -> dict[str, str | dict]: + json_data: dict[str, str | dict] = { 'id': str(self.id), 'glosses': self.glosses.get_json(), 'claims': self.claims.get_json() diff --git a/wikibaseintegrator/models/sitelinks.py b/wikibaseintegrator/models/sitelinks.py index cdb3fa00..e325372c 100644 --- a/wikibaseintegrator/models/sitelinks.py +++ b/wikibaseintegrator/models/sitelinks.py @@ -1,26 +1,24 @@ from __future__ import annotations -from typing import Dict, List, Optional - from wikibaseintegrator.models.basemodel import BaseModel class Sitelinks(BaseModel): def __init__(self) -> None: - self.sitelinks: Dict[str, Sitelink] = {} + self.sitelinks: dict[str, Sitelink] = {} - def get(self, site: Optional[str] = None) -> Optional[Sitelink]: + def get(self, site: str | None = None) -> Sitelink | None: if site in self.sitelinks: return self.sitelinks[site] return None - def set(self, site: str, title: Optional[str] = None, badges: Optional[List[str]] = None) -> Sitelink: + def set(self, site: str, title: str | None = None, badges: list[str] | None = None) -> Sitelink: sitelink = Sitelink(site, title, badges) self.sitelinks[site] = sitelink return sitelink - def from_json(self, json_data: Dict[str, Dict]) -> Sitelinks: + def from_json(self, json_data: dict[str, dict]) -> Sitelinks: for sitelink in json_data: self.set(site=json_data[sitelink]['site'], title=json_data[sitelink]['title'], badges=json_data[sitelink]['badges']) @@ -31,10 +29,10 @@ def __len__(self): class Sitelink(BaseModel): - def __init__(self, site: Optional[str] = None, title: Optional[str] = None, badges: Optional[List[str]] = None): + def __init__(self, site: str | None = None, title: str | None = None, badges: list[str] | None = None): self.site = site self.title = title - self.badges: List[str] = badges or [] + self.badges: list[str] = badges or [] def __str__(self): return self.title diff --git a/wikibaseintegrator/models/snaks.py b/wikibaseintegrator/models/snaks.py index 0388b7af..1ef40504 100644 --- a/wikibaseintegrator/models/snaks.py +++ b/wikibaseintegrator/models/snaks.py @@ -1,7 +1,7 @@ from __future__ import annotations import re -from typing import Any, Dict, List, Optional +from typing import Any from wikibaseintegrator.models.basemodel import BaseModel from wikibaseintegrator.wbi_enums import WikibaseSnakType @@ -9,9 +9,9 @@ class Snaks(BaseModel): def __init__(self) -> None: - self.snaks: Dict[str, List[Snak]] = {} + self.snaks: dict[str, list[Snak]] = {} - def get(self, property: str) -> List[Snak]: + def get(self, property: str) -> list[Snak]: return self.snaks[property] def add(self, snak: Snak) -> Snaks: @@ -24,15 +24,15 @@ def add(self, snak: Snak) -> Snaks: return self - def from_json(self, json_data: Dict[str, List]) -> Snaks: + def from_json(self, json_data: dict[str, list]) -> Snaks: for property in json_data: for snak in json_data[property]: self.add(snak=Snak().from_json(snak)) return self - def get_json(self) -> Dict[str, List]: - json_data: Dict[str, List] = {} + def get_json(self) -> dict[str, list]: + json_data: dict[str, list] = {} for property, snaks in self.snaks.items(): if property not in json_data: json_data[property] = [] @@ -51,7 +51,7 @@ def __len__(self): class Snak(BaseModel): - def __init__(self, snaktype: WikibaseSnakType = WikibaseSnakType.KNOWN_VALUE, property_number: Optional[str] = None, hash: Optional[str] = None, datavalue: Optional[Dict] = None, datatype: Optional[str] = None): + def __init__(self, snaktype: WikibaseSnakType = WikibaseSnakType.KNOWN_VALUE, property_number: str | None = None, hash: str | None = None, datavalue: dict | None = None, datatype: str | None = None): self.snaktype = snaktype self.property_number = property_number self.hash = hash @@ -112,7 +112,7 @@ def datatype(self): def datatype(self, value): self.__datatype = value - def from_json(self, json_data: Dict[str, Any]) -> Snak: + def from_json(self, json_data: dict[str, Any]) -> Snak: self.snaktype: WikibaseSnakType = WikibaseSnakType(json_data['snaktype']) self.property_number = json_data['property'] if 'hash' in json_data: @@ -123,7 +123,7 @@ def from_json(self, json_data: Dict[str, Any]) -> Snak: self.datatype = json_data['datatype'] return self - def get_json(self) -> Dict[str, str]: + def get_json(self) -> dict[str, str]: json_data = { 'snaktype': self.snaktype.value, 'property': self.property_number, diff --git a/wikibaseintegrator/wbi_fastrun.py b/wikibaseintegrator/wbi_fastrun.py index c3ed67ce..ae47bb9c 100644 --- a/wikibaseintegrator/wbi_fastrun.py +++ b/wikibaseintegrator/wbi_fastrun.py @@ -6,7 +6,7 @@ from collections import defaultdict from functools import lru_cache from itertools import chain -from typing import TYPE_CHECKING, Dict, List, Optional, Set, Type, Union +from typing import TYPE_CHECKING from wikibaseintegrator.datatypes import BaseDataType from wikibaseintegrator.models import Claim @@ -19,22 +19,22 @@ log = logging.getLogger(__name__) -fastrun_store: List[FastRunContainer] = [] +fastrun_store: list[FastRunContainer] = [] class FastRunContainer: - def __init__(self, base_data_type: Type[BaseDataType], mediawiki_api_url: Optional[str] = None, sparql_endpoint_url: Optional[str] = None, wikibase_url: Optional[str] = None, - base_filter: Optional[List[BaseDataType | List[BaseDataType]]] = None, use_refs: bool = False, case_insensitive: bool = False): - self.reconstructed_statements: List[BaseDataType] = [] - self.rev_lookup: defaultdict[str, Set[str]] = defaultdict(set) - self.rev_lookup_ci: defaultdict[str, Set[str]] = defaultdict(set) - self.prop_data: Dict[str, Dict] = {} - self.loaded_langs: Dict[str, Dict] = {} - self.base_filter: List[BaseDataType | List[BaseDataType]] = [] + def __init__(self, base_data_type: type[BaseDataType], mediawiki_api_url: str | None = None, sparql_endpoint_url: str | None = None, wikibase_url: str | None = None, + base_filter: list[BaseDataType | list[BaseDataType]] | None = None, use_refs: bool = False, case_insensitive: bool = False): + self.reconstructed_statements: list[BaseDataType] = [] + self.rev_lookup: defaultdict[str, set[str]] = defaultdict(set) + self.rev_lookup_ci: defaultdict[str, set[str]] = defaultdict(set) + self.prop_data: dict[str, dict] = {} + self.loaded_langs: dict[str, dict] = {} + self.base_filter: list[BaseDataType | list[BaseDataType]] = [] self.base_filter_string = '' - self.prop_dt_map: Dict[str, str] = {} + self.prop_dt_map: dict[str, str] = {} - self.base_data_type: Type[BaseDataType] = base_data_type + self.base_data_type: type[BaseDataType] = base_data_type self.mediawiki_api_url: str = str(mediawiki_api_url or config['MEDIAWIKI_API_URL']) self.sparql_endpoint_url: str = str(sparql_endpoint_url or config['SPARQL_ENDPOINT_URL']) self.wikibase_url: str = str(wikibase_url or config['WIKIBASE_URL']) @@ -62,8 +62,8 @@ def __init__(self, base_data_type: Type[BaseDataType], mediawiki_api_url: Option else: raise ValueError("base_filter must be an instance of BaseDataType or a list of instances of BaseDataType") - def reconstruct_statements(self, qid: str) -> List[BaseDataType]: - reconstructed_statements: List[BaseDataType] = [] + def reconstruct_statements(self, qid: str) -> list[BaseDataType]: + reconstructed_statements: list[BaseDataType] = [] if qid not in self.prop_data: self.reconstructed_statements = reconstructed_statements @@ -110,7 +110,7 @@ def reconstruct_statements(self, qid: str) -> List[BaseDataType]: self.reconstructed_statements = reconstructed_statements return reconstructed_statements - def get_items(self, claims: Union[List[Claim], Claims, Claim], cqid: Optional[str] = None) -> Optional[Set[str]]: + def get_items(self, claims: list[Claim] | Claims | Claim, cqid: str | None = None) -> set[str] | None: """ Get items ID from a SPARQL endpoint @@ -173,7 +173,7 @@ def get_items(self, claims: Union[List[Claim], Claims, Claim], cqid: Optional[st return matching_qids - def get_item(self, claims: Union[List[Claim], Claims, Claim], cqid: Optional[str] = None) -> Optional[str]: + def get_item(self, claims: list[Claim] | Claims | Claim, cqid: str | None = None) -> str | None: """ :param claims: A list of claims the entity should have @@ -181,7 +181,7 @@ def get_item(self, claims: Union[List[Claim], Claims, Claim], cqid: Optional[str :return: An entity ID, None if there is more than one. """ - matching_qids: Optional[Set[str]] = self.get_items(claims=claims, cqid=cqid) + matching_qids: set[str] | None = self.get_items(claims=claims, cqid=cqid) if matching_qids is None: return None @@ -194,7 +194,7 @@ def get_item(self, claims: Union[List[Claim], Claims, Claim], cqid: Optional[str return matching_qids.pop() - def write_required(self, data: List[Claim], action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL, cqid: Optional[str] = None) -> bool: + def write_required(self, data: list[Claim], action_if_exists: ActionIfExists = ActionIfExists.REPLACE_ALL, cqid: str | None = None) -> bool: """ Check if a write is required @@ -312,7 +312,7 @@ def init_language_data(self, lang: str, lang_data_type: str) -> None: data = self._process_lang(result=result) self.loaded_langs[lang].update({lang_data_type: data}) - def get_language_data(self, qid: str, lang: str, lang_data_type: str) -> List[str]: + def get_language_data(self, qid: str, lang: str, lang_data_type: str) -> list[str]: """ get language data for specified qid @@ -333,7 +333,7 @@ def get_language_data(self, qid: str, lang: str, lang_data_type: str) -> List[st all_lang_strings = [''] return all_lang_strings - def check_language_data(self, qid: str, lang_data: List, lang: str, lang_data_type: str, action_if_exists: ActionIfExists = ActionIfExists.APPEND_OR_REPLACE) -> bool: + def check_language_data(self, qid: str, lang_data: list, lang: str, lang_data_type: str, action_if_exists: ActionIfExists = ActionIfExists.APPEND_OR_REPLACE) -> bool: """ Method to check if certain language data exists as a label, description or aliases :param qid: Wikibase item id @@ -355,10 +355,10 @@ def check_language_data(self, qid: str, lang_data: List, lang: str, lang_data_ty return False - def get_all_data(self) -> Dict[str, Dict]: + def get_all_data(self) -> dict[str, dict]: return self.prop_data - def format_query_results(self, r: List, prop_nr: str) -> None: + def format_query_results(self, r: list, prop_nr: str) -> None: """ `r` is the results of the sparql query in _query_data and is modified in place `prop_nr` is needed to get the property datatype to determine how to format the value @@ -440,7 +440,7 @@ def format_query_results(self, r: List, prop_nr: str) -> None: else: i['rval'] = i['rval']['value'] - def update_frc_from_query(self, r: List, prop_nr: str) -> None: + def update_frc_from_query(self, r: list, prop_nr: str) -> None: # r is the output of format_query_results # this updates the frc from the query (result of _query_data) for i in r: @@ -574,7 +574,7 @@ def _query_data(self, prop_nr: str, use_units: bool = False, page_size: int = 10 if len(results) == 0 or len(results) < page_size: break - def _query_lang(self, lang: str, lang_data_type: str) -> Optional[List[Dict[str, Dict]]]: + def _query_lang(self, lang: str, lang_data_type: str) -> list[dict[str, dict]] | None: """ :param lang: @@ -603,7 +603,7 @@ def _query_lang(self, lang: str, lang_data_type: str) -> Optional[List[Dict[str, return execute_sparql_query(query=query, endpoint=self.sparql_endpoint_url)['results']['bindings'] @staticmethod - def _process_lang(result: List) -> defaultdict[str, set]: + def _process_lang(result: list) -> defaultdict[str, set]: data = defaultdict(set) for r in result: qid = r['item']['value'].split("/")[-1] @@ -612,7 +612,7 @@ def _process_lang(result: List) -> defaultdict[str, set]: return data @lru_cache(maxsize=100000) - def get_prop_datatype(self, prop_nr: str) -> Optional[str]: # pylint: disable=no-self-use + def get_prop_datatype(self, prop_nr: str) -> str | None: # pylint: disable=no-self-use from wikibaseintegrator import WikibaseIntegrator wbi = WikibaseIntegrator() property = wbi.property.get(prop_nr) @@ -639,7 +639,7 @@ def __repr__(self) -> str: ) -def get_fastrun_container(base_filter: Optional[List[BaseDataType | List[BaseDataType]]] = None, use_refs: bool = False, case_insensitive: bool = False) -> FastRunContainer: +def get_fastrun_container(base_filter: list[BaseDataType | list[BaseDataType]] | None = None, use_refs: bool = False, case_insensitive: bool = False) -> FastRunContainer: if base_filter is None: base_filter = [] @@ -649,7 +649,7 @@ def get_fastrun_container(base_filter: Optional[List[BaseDataType | List[BaseDat return fastrun_container -def _search_fastrun_store(base_filter: Optional[List[BaseDataType | List[BaseDataType]]] = None, use_refs: bool = False, case_insensitive: bool = False) -> FastRunContainer: +def _search_fastrun_store(base_filter: list[BaseDataType | list[BaseDataType]] | None = None, use_refs: bool = False, case_insensitive: bool = False) -> FastRunContainer: for fastrun in fastrun_store: if (fastrun.base_filter == base_filter) and (fastrun.use_refs == use_refs) and (fastrun.case_insensitive == case_insensitive) and ( fastrun.sparql_endpoint_url == config['SPARQL_ENDPOINT_URL']): diff --git a/wikibaseintegrator/wbi_helpers.py b/wikibaseintegrator/wbi_helpers.py index 037347ca..f50fb5a5 100644 --- a/wikibaseintegrator/wbi_helpers.py +++ b/wikibaseintegrator/wbi_helpers.py @@ -8,7 +8,7 @@ import logging import re from time import sleep -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Any from urllib.parse import urlparse import requests @@ -48,8 +48,7 @@ class BColors: default_session = requests.Session() -def mediawiki_api_call(method: str, mediawiki_api_url: Optional[str] = None, session: Optional[Session] = None, max_retries: int = 100, retry_after: int = 60, - **kwargs: Any) -> Dict: +def mediawiki_api_call(method: str, mediawiki_api_url: str | None = None, session: Session | None = None, max_retries: int = 100, retry_after: int = 60, **kwargs: Any) -> dict: """ A function to call the MediaWiki API. @@ -140,9 +139,8 @@ def mediawiki_api_call(method: str, mediawiki_api_url: Optional[str] = None, ses return json_data -def mediawiki_api_call_helper(data: Dict[str, Any], login: Optional[_Login] = None, mediawiki_api_url: Optional[str] = None, user_agent: Optional[str] = None, - allow_anonymous: bool = False, - max_retries: int = 1000, retry_after: int = 60, maxlag: int = 5, is_bot: bool = False, **kwargs: Any) -> Dict: +def mediawiki_api_call_helper(data: dict[str, Any], login: _Login | None = None, mediawiki_api_url: str | None = None, user_agent: str | None = None, allow_anonymous: bool = False, + max_retries: int = 1000, retry_after: int = 60, maxlag: int = 5, is_bot: bool = False, **kwargs: Any) -> dict: """ A simplified function to call the MediaWiki API. Pass the data, as a dictionary, related to the action you want to call, all commons options will be automatically managed. @@ -218,8 +216,8 @@ def mediawiki_api_call_helper(data: Dict[str, Any], login: Optional[_Login] = No @wbi_backoff() -def execute_sparql_query(query: str, prefix: Optional[str] = None, endpoint: Optional[str] = None, user_agent: Optional[str] = None, max_retries: int = 1000, - retry_after: int = 60) -> Dict[str, Dict]: +def execute_sparql_query(query: str, prefix: str | None = None, endpoint: str | None = None, user_agent: str | None = None, max_retries: int = 1000, retry_after: int = 60) -> dict[ + str, dict]: """ Static method which can be used to execute any SPARQL query @@ -281,8 +279,8 @@ def execute_sparql_query(query: str, prefix: Optional[str] = None, endpoint: Opt raise Exception(f"No result after {max_retries} retries.") -def edit_entity(data: Dict, id: Optional[str] = None, type: Optional[str] = None, baserevid: Optional[int] = None, summary: Optional[str] = None, clear: bool = False, - is_bot: bool = False, tags: Optional[List[str]] = None, site: Optional[str] = None, title: Optional[str] = None, **kwargs: Any) -> Dict: +def edit_entity(data: dict, id: str | None = None, type: str | None = None, baserevid: int | None = None, summary: str | None = None, clear: bool = False, is_bot: bool = False, + tags: list[str] | None = None, site: str | None = None, title: str | None = None, **kwargs: Any) -> dict: """ Creates a single new Wikibase entity and modifies it with serialised information. @@ -335,7 +333,7 @@ def edit_entity(data: Dict, id: Optional[str] = None, type: Optional[str] = None return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs) -def merge_items(from_id: str, to_id: str, login: Optional[_Login] = None, ignore_conflicts: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict: +def merge_items(from_id: str, to_id: str, login: _Login | None = None, ignore_conflicts: list[str] | None = None, is_bot: bool = False, **kwargs: Any) -> dict: """ A static method to merge two items @@ -362,7 +360,7 @@ def merge_items(from_id: str, to_id: str, login: Optional[_Login] = None, ignore return mediawiki_api_call_helper(data=params, login=login, is_bot=is_bot, **kwargs) -def merge_lexemes(source: str, target: str, login: Optional[_Login] = None, summary: Optional[str] = None, is_bot: bool = False, **kwargs: Any) -> Dict: +def merge_lexemes(source: str, target: str, login: _Login | None = None, summary: str | None = None, is_bot: bool = False, **kwargs: Any) -> dict: """ A static method to merge two lexemes @@ -389,7 +387,7 @@ def merge_lexemes(source: str, target: str, login: Optional[_Login] = None, summ return mediawiki_api_call_helper(data=params, login=login, is_bot=is_bot, **kwargs) -def remove_claims(claim_id: str, summary: Optional[str] = None, baserevid: Optional[int] = None, is_bot: bool = False, **kwargs: Any) -> Dict: +def remove_claims(claim_id: str, summary: str | None = None, baserevid: int | None = None, is_bot: bool = False, **kwargs: Any) -> dict: """ Delete a claim from an entity @@ -399,7 +397,7 @@ def remove_claims(claim_id: str, summary: Optional[str] = None, baserevid: Optio :param is_bot: Mark this edit as bot. """ - params: Dict[str, Union[str, int]] = { + params: dict[str, str | int] = { 'action': 'wbremoveclaims', 'claim': claim_id, 'format': 'json' @@ -417,8 +415,8 @@ def remove_claims(claim_id: str, summary: Optional[str] = None, baserevid: Optio return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs) -def delete_page(title: Optional[str] = None, pageid: Optional[int] = None, reason: Optional[str] = None, deletetalk: bool = False, watchlist: str = 'preferences', - watchlistexpiry: Optional[str] = None, login: Optional[_Login] = None, **kwargs: Any) -> Dict: +def delete_page(title: str | None = None, pageid: int | None = None, reason: str | None = None, deletetalk: bool = False, watchlist: str = 'preferences', + watchlistexpiry: str | None = None, login: _Login | None = None, **kwargs: Any) -> dict: """ Delete a page @@ -443,7 +441,7 @@ def delete_page(title: Optional[str] = None, pageid: Optional[int] = None, reaso if pageid and not isinstance(pageid, int): raise ValueError("pageid must be an integer.") - params: Dict[str, Any] = { + params: dict[str, Any] = { 'action': 'delete', 'watchlist': watchlist, 'format': 'json' @@ -467,8 +465,8 @@ def delete_page(title: Optional[str] = None, pageid: Optional[int] = None, reaso return mediawiki_api_call_helper(data=params, login=login, **kwargs) -def search_entities(search_string: str, language: Optional[str] = None, strict_language: bool = False, search_type: str = 'item', max_results: int = 50, dict_result: bool = False, - allow_anonymous: bool = True, **kwargs: Any) -> List[Dict[str, Any]]: +def search_entities(search_string: str, language: str | None = None, strict_language: bool = False, search_type: str = 'item', max_results: int = 50, dict_result: bool = False, + allow_anonymous: bool = True, **kwargs: Any) -> list[dict[str, Any]]: """ Performs a search for entities in the Wikibase instance using labels and aliases. You can have more information on the parameters in the MediaWiki API help (https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentities) @@ -533,7 +531,7 @@ def search_entities(search_string: str, language: Optional[str] = None, strict_l return results -def lexeme_add_form(lexeme_id, data, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict: +def lexeme_add_form(lexeme_id, data, baserevid: int | None = None, tags: list[str] | None = None, is_bot: bool = False, **kwargs: Any) -> dict: """ Adds Form to Lexeme @@ -566,7 +564,7 @@ def lexeme_add_form(lexeme_id, data, baserevid: Optional[int] = None, tags: Opti return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs) -def lexeme_edit_form(form_id: str, data, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict: +def lexeme_edit_form(form_id: str, data, baserevid: int | None = None, tags: list[str] | None = None, is_bot: bool = False, **kwargs: Any) -> dict: """ Edits representations and grammatical features of a Form @@ -607,7 +605,7 @@ def lexeme_edit_form(form_id: str, data, baserevid: Optional[int] = None, tags: return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs) -def lexeme_remove_form(form_id: str, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict: +def lexeme_remove_form(form_id: str, baserevid: int | None = None, tags: list[str] | None = None, is_bot: bool = False, **kwargs: Any) -> dict: """ Removes Form from Lexeme @@ -646,7 +644,7 @@ def lexeme_remove_form(form_id: str, baserevid: Optional[int] = None, tags: Opti return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs) -def lexeme_add_sense(lexeme_id, data, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict: +def lexeme_add_sense(lexeme_id, data, baserevid: int | None = None, tags: list[str] | None = None, is_bot: bool = False, **kwargs: Any) -> dict: """ Adds a Sense to a Lexeme @@ -679,7 +677,7 @@ def lexeme_add_sense(lexeme_id, data, baserevid: Optional[int] = None, tags: Opt return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs) -def lexeme_edit_sense(sense_id: str, data, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict: +def lexeme_edit_sense(sense_id: str, data, baserevid: int | None = None, tags: list[str] | None = None, is_bot: bool = False, **kwargs: Any) -> dict: """ Edits glosses of a Sense @@ -720,7 +718,7 @@ def lexeme_edit_sense(sense_id: str, data, baserevid: Optional[int] = None, tags return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs) -def lexeme_remove_sense(sense_id: str, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict: +def lexeme_remove_sense(sense_id: str, baserevid: int | None = None, tags: list[str] | None = None, is_bot: bool = False, **kwargs: Any) -> dict: """ Adds Form to Lexeme @@ -759,7 +757,7 @@ def lexeme_remove_sense(sense_id: str, baserevid: Optional[int] = None, tags: Op return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs) -def fulltext_search(search: str, max_results: int = 50, allow_anonymous: bool = True, **kwargs: Any) -> List[Dict[str, Any]]: +def fulltext_search(search: str, max_results: int = 50, allow_anonymous: bool = True, **kwargs: Any) -> list[dict[str, Any]]: """ Perform a fulltext search on the mediawiki instance. It's an exception to the "only wikibase related function" rule! WikibaseIntegrator is focused on wikibase-only functions to avoid spreading out and covering all functions of MediaWiki. @@ -781,7 +779,7 @@ def fulltext_search(search: str, max_results: int = 50, allow_anonymous: bool = return mediawiki_api_call_helper(data=params, allow_anonymous=allow_anonymous, **kwargs)['query']['search'] -def generate_entity_instances(entities: Union[str, List[str]], allow_anonymous: bool = True, **kwargs: Any) -> List[Tuple[str, BaseEntity]]: +def generate_entity_instances(entities: str | list[str], allow_anonymous: bool = True, **kwargs: Any) -> list[tuple[str, BaseEntity]]: """ A method which allows for retrieval of a list of Wikidata entities. The method generates a list of tuples where the first value in the tuple is the entity's ID, whereas the second is the new instance of a subclass of BaseEntity containing all the data of the entity. This is most useful for mass retrieval of entities. @@ -817,7 +815,7 @@ def generate_entity_instances(entities: Union[str, List[str]], allow_anonymous: return entity_instances -def format_amount(amount: Union[int, str, float]) -> str: +def format_amount(amount: int | str | float) -> str: """ A formatting function mostly used for Quantity datatype. :param amount: A int, float or str you want to pass to Quantity value. @@ -835,7 +833,7 @@ def format_amount(amount: Union[int, str, float]) -> str: return str(amount) -def get_user_agent(user_agent: Optional[str]) -> str: +def get_user_agent(user_agent: str | None) -> str: """ Return a user agent string suitable for interacting with the Wikibase instance. @@ -856,7 +854,7 @@ def get_user_agent(user_agent: Optional[str]) -> str: properties_dt: dict = {} -def format2wbi(entitytype: str, json_raw: str, allow_anonymous: bool = True, wikibase_url: Optional[str] = None, **kwargs) -> BaseEntity: +def format2wbi(entitytype: str, json_raw: str, allow_anonymous: bool = True, wikibase_url: str | None = None, **kwargs) -> BaseEntity: wikibase_url = str(wikibase_url or config['WIKIBASE_URL']) json_decoded = json.loads(json_raw) # pprint(json_decoded) @@ -940,7 +938,7 @@ def format2wbi(entitytype: str, json_raw: str, allow_anonymous: bool = True, wik return entity -def _json2datatype(prop_nr: str, statement: dict, wikibase_url: Optional[str] = None, allow_anonymous=True, **kwargs) -> BaseDataType: +def _json2datatype(prop_nr: str, statement: dict, wikibase_url: str | None = None, allow_anonymous=True, **kwargs) -> BaseDataType: from wikibaseintegrator.datatypes.basedatatype import BaseDataType wikibase_url = str(wikibase_url or config['WIKIBASE_URL']) @@ -984,7 +982,8 @@ def _json2datatype(prop_nr: str, statement: dict, wikibase_url: Optional[str] = precision = statement['precision'] or None timezone = statement['timezone'] or 0 calendarmodel = statement['calendarmodel'] or None - return f(prop_nr=prop_nr, time=statement['time'], before=before, after=after, precision=precision, timezone=timezone, calendarmodel=calendarmodel, wikibase_url=wikibase_url) + return f(prop_nr=prop_nr, time=statement['time'], before=before, after=after, precision=precision, timezone=timezone, calendarmodel=calendarmodel, + wikibase_url=wikibase_url) return f() diff --git a/wikibaseintegrator/wikibaseintegrator.py b/wikibaseintegrator/wikibaseintegrator.py index 0316e61f..e8ca2db6 100644 --- a/wikibaseintegrator/wikibaseintegrator.py +++ b/wikibaseintegrator/wikibaseintegrator.py @@ -3,7 +3,7 @@ """ from __future__ import annotations -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING from wikibaseintegrator.entities.item import ItemEntity from wikibaseintegrator.entities.lexeme import LexemeEntity @@ -16,7 +16,7 @@ class WikibaseIntegrator: - def __init__(self, is_bot: bool = False, login: Optional[_Login] = None): + def __init__(self, is_bot: bool = False, login: _Login | None = None): """ This function initializes a WikibaseIntegrator instance to quickly access different entity type instances.