From 268acfdebded0a0330634fcf17eb1bdaa0405832 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 7 May 2022 00:54:26 +0530 Subject: [PATCH 1/7] fix: ci supports mypy now --- .github/workflows/lint.yml | 17 +++++++++++++++++ json2xml/json2xml.py | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index afcabb1c..24c617c6 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -41,3 +41,20 @@ jobs: with: linters: isort run: isort --check --diff json2xml + + mypy: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: 3.9 + cache: 'pip' + - run: python -m pip install mypy && mypy --install-types + - name: mypy + uses: liskin/gh-problem-matcher-wrap@v1 + with: + linters: mypy json2xml + diff --git a/json2xml/json2xml.py b/json2xml/json2xml.py index 1311fd87..7d2d6b10 100644 --- a/json2xml/json2xml.py +++ b/json2xml/json2xml.py @@ -1,4 +1,4 @@ -from typing import Any, Optional +from typing import Any, Dict, Optional from defusedxml.minidom import parseString from pyexpat import ExpatError @@ -14,7 +14,7 @@ class Json2xml: """ def __init__( self, - data: str, + data: Dict[str, Any], wrapper: str = "all", root: bool = True, pretty: bool = True, From d9677fcc7192b3d1b8598c2f35281ff176611f94 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 7 May 2022 00:57:37 +0530 Subject: [PATCH 2/7] fix: specify types stubs to install --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 24c617c6..9c8cb58a 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -52,7 +52,7 @@ jobs: with: python-version: 3.9 cache: 'pip' - - run: python -m pip install mypy && mypy --install-types + - run: python -m pip install mypy types-requests==2.27.25 types-urllib3==1.26.14 - name: mypy uses: liskin/gh-problem-matcher-wrap@v1 with: From e36f0420e39b924e8cf36743c25dfc2496f916d7 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 7 May 2022 01:02:17 +0530 Subject: [PATCH 3/7] fix: use correct lint --- .github/workflows/lint.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 9c8cb58a..bd77b461 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -42,7 +42,10 @@ jobs: linters: isort run: isort --check --diff json2xml + + mypy: + name: mypy runs-on: ubuntu-latest steps: - name: Checkout @@ -52,9 +55,11 @@ jobs: with: python-version: 3.9 cache: 'pip' - - run: python -m pip install mypy types-requests==2.27.25 types-urllib3==1.26.14 + - run: pip install --upgrade mypy types-requests types-urllib3 - name: mypy uses: liskin/gh-problem-matcher-wrap@v1 with: - linters: mypy json2xml + linters: mypy + run: | + mypy json2xml From f4c16c97190105cb6eb3db45561cc9fb044cbf3a Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 7 May 2022 01:17:55 +0530 Subject: [PATCH 4/7] fix: use modern format for type hints --- json2xml/dicttoxml.py | 36 ++++++++++++++++++------------------ json2xml/json2xml.py | 6 ++++-- json2xml/utils.py | 8 +++++--- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/json2xml/dicttoxml.py b/json2xml/dicttoxml.py index 41f751da..654a05a8 100755 --- a/json2xml/dicttoxml.py +++ b/json2xml/dicttoxml.py @@ -25,7 +25,7 @@ LOG = logging.getLogger("dicttoxml") # pragma: no cover -ids: List[str] = [] # initialize list of unique ids +ids: list[str] = [] # initialize list of unique ids def make_id(element: str, start: int = 100000, end: int = 999999) -> str: @@ -82,7 +82,7 @@ def get_xml_type(val: ELEMENT) -> str: return type(val).__name__ -def escape_xml(s: Union[str, numbers.Number]) -> str: +def escape_xml(s: str | numbers.Number) -> str: if isinstance(s, str): s = str(s) # avoid UnicodeDecodeError s = s.replace("&", "&") @@ -111,7 +111,7 @@ def key_is_valid_xml(key: str) -> bool: return False -def make_valid_xml_name(key: str, attr: Dict[str, Any]) -> Tuple[str, Dict[str, Any]]: +def make_valid_xml_name(key: str, attr: dict[str, Any]) -> tuple[str, dict[str, Any]]: """Tests an XML name and fixes it if invalid""" if DEBUGMODE: # pragma: no cover LOG.info( @@ -144,7 +144,7 @@ def make_valid_xml_name(key: str, attr: Dict[str, Any]) -> Tuple[str, Dict[str, return key, attr -def wrap_cdata(s: Union[str, numbers.Number]) -> str: +def wrap_cdata(s: str | numbers.Number) -> str: """Wraps a string into CDATA sections""" s = str(s).replace("]]>", "]]]]>") return "" @@ -219,8 +219,8 @@ def is_primitive_type(val: Any) -> bool: def dict2xml_str( attr_type: bool, - attr: Dict[str, Any], - item: Dict[str, Any], + attr: dict[str, Any], + item: dict[str, Any], item_func: Callable[[str], str], cdata: bool, item_name: str, @@ -258,7 +258,7 @@ def dict2xml_str( def list2xml_str( attr_type: bool, - attr: Dict[str, Any], + attr: dict[str, Any], item: Sequence[Any], item_func: Callable[[str], str], cdata: bool, @@ -287,8 +287,8 @@ def list2xml_str( def convert_dict( - obj: Dict[str, Any], - ids: List[str], + obj: dict[str, Any], + ids: list[str], parent: str, attr_type: bool, item_func: Callable[[str], str], @@ -305,7 +305,7 @@ def convert_dict( if LOG.getEffectiveLevel() <= logging.DEBUG: LOG.debug(f' obj="{str(obj)}"') - output: List[str] = [] + output: list[str] = [] addline = output.append for key, val in obj.items(): @@ -376,7 +376,7 @@ def convert_dict( def convert_list( items: Sequence[Any], - ids: List[str], + ids: list[str], parent: str, attr_type: bool, item_func: Callable[[str], str], @@ -390,7 +390,7 @@ def convert_list( if LOG.getEffectiveLevel() <= logging.DEBUG: LOG.debug(f' items="{str(items)}"') - output: List[str] = [] + output: list[str] = [] addline = output.append item_name = item_func(parent) @@ -477,9 +477,9 @@ def convert_list( def convert_kv( key: str, - val: Union[str, numbers.Number], + val: str | numbers.Number, attr_type: bool, - attr: Dict[str, Any] = {}, + attr: dict[str, Any] = {}, cdata: bool = False, ) -> str: """Converts a number or string into an XML element""" @@ -496,7 +496,7 @@ def convert_kv( def convert_bool( - key: str, val: bool, attr_type: bool, attr: Dict[str, Any] = {}, cdata: bool = False + key: str, val: bool, attr_type: bool, attr: dict[str, Any] = {}, cdata: bool = False ) -> str: """Converts a boolean into an XML element""" if DEBUGMODE: # pragma: no cover @@ -512,7 +512,7 @@ def convert_bool( def convert_none( - key: str, attr_type: bool, attr: Dict[str, Any] = {}, cdata: bool = False + key: str, attr_type: bool, attr: dict[str, Any] = {}, cdata: bool = False ) -> str: """Converts a null value into an XML element""" key, attr = make_valid_xml_name(key, attr) @@ -524,10 +524,10 @@ def convert_none( def dicttoxml( - obj: Dict[str, Any], + obj: dict[str, Any], root: bool = True, custom_root: str = "root", - ids: Optional[List[int]] = None, + ids: list[int] | None = None, attr_type: bool = True, item_wrap: bool = True, item_func: Callable[[str], str] = default_item_func, diff --git a/json2xml/json2xml.py b/json2xml/json2xml.py index 7d2d6b10..f83bcf20 100644 --- a/json2xml/json2xml.py +++ b/json2xml/json2xml.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from typing import Any, Dict, Optional from defusedxml.minidom import parseString @@ -14,7 +16,7 @@ class Json2xml: """ def __init__( self, - data: Dict[str, Any], + data: dict[str, Any], wrapper: str = "all", root: bool = True, pretty: bool = True, @@ -28,7 +30,7 @@ def __init__( self.root = root self.item_wrap = item_wrap - def to_xml(self) -> Optional[Any]: + def to_xml(self) -> Any | None: """ Convert to xml using dicttoxml.dicttoxml and then pretty print it. """ diff --git a/json2xml/utils.py b/json2xml/utils.py index 9b6f34d4..93d95612 100644 --- a/json2xml/utils.py +++ b/json2xml/utils.py @@ -1,3 +1,5 @@ +from __future__ import annotations + """Utils methods to convert XML data to dict from various sources""" import json from typing import Dict, Optional @@ -21,7 +23,7 @@ class StringReadError(Exception): pass -def readfromjson(filename: str) -> Dict[str, str]: +def readfromjson(filename: str) -> dict[str, str]: """ Reads a json string and emits json string """ @@ -38,7 +40,7 @@ def readfromjson(filename: str) -> Dict[str, str]: raise JSONReadError("Invalid JSON File") -def readfromurl(url: str, params: Optional[Dict[str, str]] = None) -> Dict[str, str]: +def readfromurl(url: str, params: dict[str, str] | None = None) -> dict[str, str]: """ Loads json from an URL over the internets """ @@ -51,7 +53,7 @@ def readfromurl(url: str, params: Optional[Dict[str, str]] = None) -> Dict[str, raise URLReadError("URL is not returning correct response") -def readfromstring(jsondata: str) -> Dict[str, str]: +def readfromstring(jsondata: str) -> dict[str, str]: """ Loads json from string """ From fd259a17707c468684b85e159a87dcb501fd7646 Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 7 May 2022 01:27:29 +0530 Subject: [PATCH 5/7] fix: remove unused imports --- json2xml/dicttoxml.py | 4 ++-- json2xml/json2xml.py | 4 ++-- json2xml/utils.py | 1 - 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/json2xml/dicttoxml.py b/json2xml/dicttoxml.py index 654a05a8..35e5c181 100755 --- a/json2xml/dicttoxml.py +++ b/json2xml/dicttoxml.py @@ -17,7 +17,7 @@ import os from collections.abc import Callable, Sequence from random import randint -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Union from defusedxml.minidom import parseString @@ -56,7 +56,7 @@ def get_unique_id(element: str) -> str: datetime.datetime, datetime.date, None, - Dict[str, Any], + dict[str, Any], ] diff --git a/json2xml/json2xml.py b/json2xml/json2xml.py index f83bcf20..bf623be4 100644 --- a/json2xml/json2xml.py +++ b/json2xml/json2xml.py @@ -1,6 +1,6 @@ -from __future__ import annotations +from __future__ import annotations -from typing import Any, Dict, Optional +from typing import Any from defusedxml.minidom import parseString from pyexpat import ExpatError diff --git a/json2xml/utils.py b/json2xml/utils.py index 93d95612..61ac0773 100644 --- a/json2xml/utils.py +++ b/json2xml/utils.py @@ -2,7 +2,6 @@ """Utils methods to convert XML data to dict from various sources""" import json -from typing import Dict, Optional import requests From 3da2d5a6a4ba5cb6610098d75d285ca589e9a5fa Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 7 May 2022 01:30:16 +0530 Subject: [PATCH 6/7] fix: failures --- json2xml/dicttoxml.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/json2xml/dicttoxml.py b/json2xml/dicttoxml.py index 35e5c181..b81ae953 100755 --- a/json2xml/dicttoxml.py +++ b/json2xml/dicttoxml.py @@ -17,7 +17,7 @@ import os from collections.abc import Callable, Sequence from random import randint -from typing import Any, Union +from typing import Any, Union, Dict from defusedxml.minidom import parseString @@ -56,7 +56,7 @@ def get_unique_id(element: str) -> str: datetime.datetime, datetime.date, None, - dict[str, Any], + Dict[str, Any], ] From fef2e6da83662804ace0f4f08a52fe666b12b81d Mon Sep 17 00:00:00 2001 From: Vinit Kumar Date: Sat, 7 May 2022 01:35:29 +0530 Subject: [PATCH 7/7] fix: failures --- json2xml/dicttoxml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json2xml/dicttoxml.py b/json2xml/dicttoxml.py index b81ae953..d274c1a6 100755 --- a/json2xml/dicttoxml.py +++ b/json2xml/dicttoxml.py @@ -17,7 +17,7 @@ import os from collections.abc import Callable, Sequence from random import randint -from typing import Any, Union, Dict +from typing import Any, Dict, Union from defusedxml.minidom import parseString