Skip to content

Commit

Permalink
Refactor docstrings 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
tefra committed Feb 13, 2024
1 parent 4403362 commit b582b2b
Show file tree
Hide file tree
Showing 120 changed files with 6,993 additions and 2,947 deletions.
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# ones.
extensions = [
"xsdatadocs",
"sphinx.ext.napoleon",
"sphinx.ext.doctest",
"sphinx.ext.autodoc",
"sphinx.ext.intersphinx",
Expand Down Expand Up @@ -86,3 +87,4 @@
autosummary_generate = True
set_type_checking_flag = True
always_document_param_types = False
napoleon_google_docstring = True
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,17 @@ ignore = [
"B028",
"B904",
"D100",
"D104",
"D107"

]

isort = { known-first-party = ['xsdata', 'tests'] }

[tool.ruff.lint.per-file-ignores]
"**/{tests}/*" = ["ANN001", "ANN002", "ANN003", "E501", "B018", "D"]
"**/utils/testing.py" = ["D"]
"docs/*" = ["D"]

[tool.ruff.lint.pydocstyle]
convention = "google"
Expand Down
4 changes: 2 additions & 2 deletions tests/codegen/handlers/test_process_attributes_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ def test_copy_attribute_properties(self, mock_copy_inner_class):
)
mock_copy_inner_class.assert_has_calls(
[
mock.call(source, target, attr, source.attrs[0].types[0]),
mock.call(source, target, attr, source.attrs[0].types[1]),
mock.call(source, target, source.attrs[0].types[0]),
mock.call(source, target, source.attrs[0].types[1]),
]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,18 @@ def test_is_valid_enum_type(self):
self.assertFalse(self.processor.is_valid_enum_type(enumeration, attr))
self.assertEqual("3", attr.default)

def test_find_type(self):
def test_find_inner_type(self):
target = ClassFactory.create()
attr_type = AttrTypeFactory.create("foo")
foo = ClassFactory.create(qname="foo")
self.processor.container.add(foo)

self.assertIs(foo, self.processor.find_type(target, attr_type))
self.assertIs(foo, self.processor.find_inner_type(target, attr_type))

attr_type = AttrTypeFactory.create("bar", forward=True)
bar = ClassFactory.create(qname="bar")
target.inner.append(bar)
self.assertIs(bar, self.processor.find_type(target, attr_type))
self.assertIs(bar, self.processor.find_inner_type(target, attr_type))

def test_reset_attribute_types(self):
attr = AttrFactory.create(
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/mappers/test_definitions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Generator
from unittest import mock

from xsdata.codegen.mappers.definitions import DefinitionsMapper
from xsdata.codegen.mappers import DefinitionsMapper
from xsdata.codegen.models import Class, Status
from xsdata.formats.dataclass.models.generics import AnyElement
from xsdata.models.enums import DataType, Namespace, Tag
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/mappers/test_dict.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from unittest import mock

from xsdata.codegen.mappers.dict import DictMapper
from xsdata.codegen.mappers import DictMapper
from xsdata.codegen.models import Restrictions
from xsdata.codegen.utils import ClassUtils
from xsdata.models.enums import DataType, Tag
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/mappers/test_dtd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Iterator
from unittest import mock

from xsdata.codegen.mappers.dtd import DtdMapper
from xsdata.codegen.mappers import DtdMapper
from xsdata.codegen.models import Class, Restrictions
from xsdata.models.dtd import (
DtdAttributeDefault,
Expand Down
16 changes: 8 additions & 8 deletions tests/codegen/mappers/test_element.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from unittest import mock

from xsdata.codegen.mappers.element import ElementMapper
from xsdata.codegen.mappers import ElementMapper
from xsdata.codegen.models import Restrictions
from xsdata.codegen.utils import ClassUtils
from xsdata.formats.dataclass.models.generics import AnyElement
Expand Down Expand Up @@ -267,28 +267,28 @@ def test_build_class_ignore_invalid(self):
actual = ElementMapper.build_class(element, None)
self.assertEqual(0, len(actual.attrs))

def test_build_attribute_type(self):
actual = ElementMapper.build_attribute_type(QNames.XSI_TYPE, "")
def test_build_attr_type(self):
actual = ElementMapper.build_attr_type(QNames.XSI_TYPE, "")
self.assertEqual(str(DataType.QNAME), actual.qname)
self.assertTrue(actual.native)

actual = ElementMapper.build_attribute_type("name", "foo")
actual = ElementMapper.build_attr_type("name", "foo")
self.assertEqual(str(DataType.STRING), actual.qname)
self.assertTrue(actual.native)

actual = ElementMapper.build_attribute_type("name", "")
actual = ElementMapper.build_attr_type("name", "")
self.assertEqual(str(DataType.ANY_SIMPLE_TYPE), actual.qname)
self.assertTrue(actual.native)

actual = ElementMapper.build_attribute_type("name", None)
actual = ElementMapper.build_attr_type("name", None)
self.assertEqual(str(DataType.ANY_SIMPLE_TYPE), actual.qname)
self.assertTrue(actual.native)

actual = ElementMapper.build_attribute_type("name", 1)
actual = ElementMapper.build_attr_type("name", 1)
self.assertEqual(str(DataType.SHORT), actual.qname)
self.assertTrue(actual.native)

actual = ElementMapper.build_attribute_type("name", "1.9")
actual = ElementMapper.build_attr_type("name", "1.9")
self.assertEqual(str(DataType.FLOAT), actual.qname)
self.assertTrue(actual.native)

Expand Down
28 changes: 12 additions & 16 deletions tests/codegen/mappers/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Iterator
from unittest import mock

from xsdata.codegen.mappers.schema import SchemaMapper
from xsdata.codegen.mappers import SchemaMapper
from xsdata.codegen.models import Class, Restrictions
from xsdata.models.enums import DataType, FormType, Tag
from xsdata.models.xsd import (
Expand Down Expand Up @@ -268,7 +268,7 @@ def test_children_extensions(self):
self.assertIsInstance(children, GeneratorType)
self.assertEqual(expected, list(children))

@mock.patch.object(SchemaMapper, "build_class_attribute_types")
@mock.patch.object(SchemaMapper, "build_attr_types")
@mock.patch.object(SchemaMapper, "element_namespace")
@mock.patch.object(Attribute, "get_restrictions")
@mock.patch.object(Attribute, "is_fixed", new_callable=mock.PropertyMock)
Expand All @@ -285,13 +285,11 @@ def test_build_class_attribute(
mock_is_fixed,
mock_get_restrictions,
mock_element_namespace,
mock_build_class_attribute_types,
mock_build_attr_types,
):
item = ClassFactory.create(ns_map={"bar": "foo"})

mock_build_class_attribute_types.return_value = AttrTypeFactory.list(
1, qname="int"
)
mock_build_attr_types.return_value = AttrTypeFactory.list(1, qname="int")
mock_real_name.return_value = item.name
mock_display_help.return_value = "sos"
mock_prefix.return_value = "com"
Expand All @@ -307,7 +305,7 @@ def test_build_class_attribute(
SchemaMapper.build_class_attribute(item, attribute, Restrictions())
expected = AttrFactory.create(
name=mock_real_name.return_value,
types=mock_build_class_attribute_types.return_value,
types=mock_build_attr_types.return_value,
tag=Tag.ATTRIBUTE,
namespace=mock_element_namespace.return_value,
help=mock_display_help.return_value,
Expand All @@ -318,20 +316,18 @@ def test_build_class_attribute(
)
self.assertEqual(expected, item.attrs[0])
self.assertEqual({"bar": "foo", "foo": "bar"}, item.ns_map)
mock_build_class_attribute_types.assert_called_once_with(item, attribute)
mock_build_attr_types.assert_called_once_with(item, attribute)
mock_element_namespace.assert_called_once_with(attribute, item.target_namespace)

@mock.patch.object(Attribute, "attr_types", new_callable=mock.PropertyMock)
@mock.patch.object(SchemaMapper, "build_inner_classes")
def test_build_class_attribute_types(
self, mock_build_inner_classes, mock_attr_types
):
def test_build_attr_types(self, mock_build_inner_classes, mock_attr_types):
mock_attr_types.return_value = ["xs:integer", "xs:string"]
mock_build_inner_classes.return_value = []

item = ClassFactory.create()
attribute = Attribute(default="false")
actual = SchemaMapper.build_class_attribute_types(item, attribute)
actual = SchemaMapper.build_attr_types(item, attribute)

expected = [
AttrTypeFactory.native(DataType.INTEGER),
Expand All @@ -342,7 +338,7 @@ def test_build_class_attribute_types(

@mock.patch.object(Attribute, "attr_types", new_callable=mock.PropertyMock)
@mock.patch.object(SchemaMapper, "build_inner_classes")
def test_build_class_attribute_types_when_obj_has_inner_class(
def test_build_attr_types_when_obj_has_inner_class(
self, mock_build_inner_classes, mock_attr_types
):
inner_class = ClassFactory.create(qname="foo")
Expand All @@ -351,7 +347,7 @@ def test_build_class_attribute_types_when_obj_has_inner_class(

item = ClassFactory.create()
attribute = Attribute(default="false")
actual = SchemaMapper.build_class_attribute_types(item, attribute)
actual = SchemaMapper.build_attr_types(item, attribute)

expected = [
AttrTypeFactory.native(DataType.INTEGER),
Expand All @@ -365,7 +361,7 @@ def test_build_class_attribute_types_when_obj_has_inner_class(
@mock.patch.object(Attribute, "default_type", new_callable=mock.PropertyMock)
@mock.patch.object(Attribute, "attr_types", new_callable=mock.PropertyMock)
@mock.patch.object(SchemaMapper, "build_inner_classes")
def test_build_class_attribute_types_when_obj_has_no_types(
def test_build_attr_types_when_obj_has_no_types(
self, mock_build_inner_classes, mock_attr_types, mock_default_type
):
mock_attr_types.return_value = ""
Expand All @@ -374,7 +370,7 @@ def test_build_class_attribute_types_when_obj_has_no_types(

item = ClassFactory.create()
attribute = Attribute(default="false", name="attr")
actual = SchemaMapper.build_class_attribute_types(item, attribute)
actual = SchemaMapper.build_attr_types(item, attribute)

self.assertEqual(1, len(actual))
self.assertEqual(AttrTypeFactory.native(DataType.STRING), actual[0])
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/parsers/test_dtd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from unittest import TestCase, mock

from tests import fixtures_dir
from xsdata.codegen.parsers.dtd import DtdParser
from xsdata.codegen.parsers import DtdParser
from xsdata.exceptions import ParserError
from xsdata.models.dtd import (
DtdAttributeDefault,
Expand Down
20 changes: 13 additions & 7 deletions tests/codegen/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ def test_apply_aliases(self):

@mock.patch.object(DependenciesResolver, "set_aliases")
@mock.patch.object(DependenciesResolver, "resolve_conflicts")
@mock.patch.object(DependenciesResolver, "find_package")
@mock.patch.object(DependenciesResolver, "get_class_module")
@mock.patch.object(DependenciesResolver, "import_classes")
def test_resolve_imports(
self,
mock_import_classes,
mock_find_package,
mock_get_class_module,
mock_resolve_conflicts,
mock_set_aliases,
):
Expand All @@ -150,7 +150,13 @@ def test_resolve_imports(
]
self.resolver.class_map = {class_life.qname: class_life}
mock_import_classes.return_value = import_names
mock_find_package.side_effect = ["first", "second", "third", "forth", "fifth"]
mock_get_class_module.side_effect = [
"first",
"second",
"third",
"forth",
"fifth",
]

self.resolver.resolve_imports()
mock_resolve_conflicts.assert_called_once_with(
Expand Down Expand Up @@ -183,13 +189,13 @@ def test_set_aliases(self):
self.resolver.set_aliases()
self.assertEqual({"{a}a": "aa", "{b}a": "ba"}, self.resolver.aliases)

def test_find_package(self):
def test_get_class_module(self):
class_a = ClassFactory.create()
self.resolver.packages[class_a.qname] = "foo.bar"
self.resolver.registry[class_a.qname] = "foo.bar"

self.assertEqual("foo.bar", self.resolver.find_package(class_a.qname))
self.assertEqual("foo.bar", self.resolver.get_class_module(class_a.qname))
with self.assertRaises(ResolverValueError):
self.resolver.find_package("nope")
self.resolver.get_class_module("nope")

def test_import_classes(self):
self.resolver.class_list = list("abcdefg")
Expand Down
Loading

0 comments on commit b582b2b

Please sign in to comment.