Skip to content

Remove pydantic and switch to dataclasses/validobj #250

New issue

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

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

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies = [
"setuptools >= 45",
"setuptools_scm >= 6.2, < 8",
"sphinxify >= 0.7.3",
"pydantic >= 1.7.0, < 2, != 1.10.20",
"validobj ~= 1.2",
"cxxheaderparser[pcpp] ~= 1.4.1",
"tomli",
"tomli_w",
Expand Down
2 changes: 1 addition & 1 deletion robotpy_build/autowrap/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ class TemplateInstanceContext:
full_cpp_name_identifier: str
binder_typename: str

params: typing.List[str]
params: typing.List[typing.Union[int, str]]

header_name: str

Expand Down
21 changes: 11 additions & 10 deletions robotpy_build/autowrap/cxxparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ def _gen_int_types():


_rvp_map = {
ReturnValuePolicy.TAKE_OWNERSHIP: "py::return_value_policy::take_ownership",
ReturnValuePolicy.COPY: "py::return_value_policy::copy",
ReturnValuePolicy.MOVE: "py::return_value_policy::move",
ReturnValuePolicy.REFERENCE: "py::return_value_policy::reference",
ReturnValuePolicy.REFERENCE_INTERNAL: "py::return_value_policy::reference_internal",
ReturnValuePolicy.AUTOMATIC: "",
ReturnValuePolicy.AUTOMATIC_REFERENCE: "py::return_value_policy::automatic_reference",
ReturnValuePolicy.take_ownership: "py::return_value_policy::take_ownership",
ReturnValuePolicy.copy: "py::return_value_policy::copy",
ReturnValuePolicy.move: "py::return_value_policy::move",
ReturnValuePolicy.reference: "py::return_value_policy::reference",
ReturnValuePolicy.reference_internal: "py::return_value_policy::reference_internal",
ReturnValuePolicy.automatic: "",
ReturnValuePolicy.automatic_reference: "py::return_value_policy::automatic_reference",
}

# fmt: off
Expand Down Expand Up @@ -938,7 +938,7 @@ def _on_class_field(
else:
py_name = prop_name

if propdata.access == PropAccess.AUTOMATIC:
if propdata.access == PropAccess.auto:
# const variables can't be written
if f.constexpr or getattr(f.type, "const", False):
prop_readonly = True
Expand All @@ -949,7 +949,7 @@ def _on_class_field(
else:
prop_readonly = _is_prop_readonly(f.type)
else:
prop_readonly = propdata.access == PropAccess.READONLY
prop_readonly = propdata.access == PropAccess.readonly

doc = self._process_doc(f.doxygen, propdata)

Expand Down Expand Up @@ -2038,7 +2038,8 @@ def parse_header(
break

for param in tmpl_data.params:
visitor._add_user_type_caster(param)
if isinstance(param, str):
visitor._add_user_type_caster(param)

# User typealias additions
visitor._extract_typealias(user_cfg.typealias, hctx.user_typealias, set())
Expand Down
24 changes: 19 additions & 5 deletions robotpy_build/autowrap/generator_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
AutowrapConfigYaml,
PropData,
FunctionData,
OverloadData,
)
from .context import OverloadTracker

Expand Down Expand Up @@ -40,6 +41,23 @@ class ClsReportData:
functions: FnMissingData = dataclasses.field(default_factory=dict)


def _merge_overload(data: FunctionData, overload: OverloadData) -> FunctionData:
# merge overload information
# - create a dictionary that contains things that haven't changed
changes = {"overloads": {}}
for f in dataclasses.fields(OverloadData):
v = getattr(overload, f.name)
if f.default_factory is not dataclasses.MISSING:
default = f.default_factory()
else:
default = f.default

if v != default:
changes[f.name] = v

return dataclasses.replace(data, **changes)


class GeneratorData:
"""
Used by the hooks to retrieve user-specified generation data, and
Expand Down Expand Up @@ -141,11 +159,7 @@ def get_function_data(
overload = data.overloads.get(signature)
missing = overload is None
if not missing and overload:
# merge overload information
data = data.dict(exclude_unset=True)
del data["overloads"]
data.update(overload.dict(exclude_unset=True))
data = FunctionData(**data)
data = _merge_overload(data, overload)
report_data.overloads[signature] = is_private or not missing

report_data.tracker.add_overload()
Expand Down
2 changes: 1 addition & 1 deletion robotpy_build/autowrap/render_tmpl_inst.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def render_template_inst_cpp(
r = RenderBuffer()
render_class_prologue(r, hctx)

tmpl_params = ", ".join(tmpl_data.params)
tmpl_params = ", ".join(str(p) for p in tmpl_data.params)

r.write_trim(
f"""
Expand Down
Loading
Loading