Skip to content

Commit 09e547f

Browse files
bonzinidcbaker
authored andcommitted
cargo: remove Fixed* dictionaries
Create the dataclasses directly from the raw dictionaries, without an intermediate step. Extracted from a patch by Xavier Classens. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent e88ab40 commit 09e547f

File tree

2 files changed

+7
-99
lines changed

2 files changed

+7
-99
lines changed

mesonbuild/cargo/interpreter.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
3636
class DataclassInstance(Protocol):
3737
__dataclass_fields__: T.ClassVar[dict[str, dataclasses.Field[T.Any]]]
3838

39-
_UnknownKeysT = T.TypeVar('_UnknownKeysT', manifest.FixedPackage,
40-
manifest.FixedDependency, manifest.FixedLibTarget,
41-
manifest.FixedBuildTarget)
42-
4339
_R = T.TypeVar('_R', bound='raw._BaseBuildTarget')
4440

4541

@@ -61,19 +57,7 @@ def fixup_meson_varname(name: str) -> str:
6157
return name.replace('-', '_')
6258

6359

64-
# Pylance can figure out that these do not, in fact, overlap, but mypy can't
65-
@T.overload
66-
def _fixup_raw_mappings(d: raw.BuildTarget) -> manifest.FixedBuildTarget: ... # type: ignore
67-
68-
@T.overload
69-
def _fixup_raw_mappings(d: raw.LibTarget) -> manifest.FixedLibTarget: ... # type: ignore
70-
71-
@T.overload
72-
def _fixup_raw_mappings(d: raw.Dependency) -> manifest.FixedDependency: ...
73-
74-
def _fixup_raw_mappings(d: T.Union[raw.BuildTarget, raw.LibTarget, raw.Dependency]
75-
) -> T.Union[manifest.FixedBuildTarget, manifest.FixedLibTarget,
76-
manifest.FixedDependency]:
60+
def _fixup_raw_mappings(d: T.Mapping[str, T.Any], convert_version: bool = True) -> T.MutableMapping[str, T.Any]:
7761
"""Fixup raw cargo mappings to ones more suitable for python to consume.
7862
7963
This does the following:
@@ -86,14 +70,14 @@ def _fixup_raw_mappings(d: T.Union[raw.BuildTarget, raw.LibTarget, raw.Dependenc
8670
:return: the fixed string
8771
"""
8872
raw = {fixup_meson_varname(k): v for k, v in d.items()}
89-
if 'version' in raw:
73+
if convert_version and 'version' in raw:
9074
assert isinstance(raw['version'], str), 'for mypy'
9175
raw['version'] = version.convert(raw['version'])
92-
return T.cast('T.Union[manifest.FixedBuildTarget, manifest.FixedLibTarget, manifest.FixedDependency]', raw)
76+
return raw
9377

9478

95-
def _handle_unknown_keys(data: _UnknownKeysT, cls: T.Union[DataclassInstance, T.Type[DataclassInstance]],
96-
msg: str) -> _UnknownKeysT:
79+
def _handle_unknown_keys(data: T.MutableMapping[str, T.Any], cls: T.Union[DataclassInstance, T.Type[DataclassInstance]],
80+
msg: str) -> T.MutableMapping[str, T.Any]:
9781
"""Remove and warn on keys that are coming from cargo, but are unknown to
9882
our representations.
9983
@@ -111,8 +95,7 @@ def _handle_unknown_keys(data: _UnknownKeysT, cls: T.Union[DataclassInstance, T.
11195
mlog.warning(msg, 'has unexpected keys', '"{}".'.format(', '.join(sorted(unexpected))),
11296
_EXTRA_KEYS_WARNING)
11397
for k in unexpected:
114-
# Mypy and Pyright can't prove that this is okay
115-
del data[k] # type: ignore[misc]
98+
del data[k]
11699
return data
117100

118101

@@ -156,8 +139,7 @@ def __post_init__(self) -> None:
156139

157140
@classmethod
158141
def from_raw(cls, raw: raw.Package) -> Self:
159-
pkg = T.cast('manifest.FixedPackage',
160-
{fixup_meson_varname(k): v for k, v in raw.items()})
142+
pkg = _fixup_raw_mappings(raw, convert_version=False)
161143
pkg = _handle_unknown_keys(pkg, cls, f'Package entry {pkg["name"]}')
162144
return cls(**pkg)
163145

mesonbuild/cargo/manifest.py

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,3 @@
44
"""Type definitions for cargo manifest files."""
55

66
from __future__ import annotations
7-
import typing as T
8-
9-
from typing_extensions import TypedDict, Required
10-
11-
from .raw import EDITION, CRATE_TYPE
12-
13-
class FixedPackage(TypedDict, total=False):
14-
15-
"""A description of the Package Dictionary, fixed up."""
16-
17-
name: Required[str]
18-
version: Required[str]
19-
authors: T.List[str]
20-
edition: EDITION
21-
rust_version: str
22-
description: str
23-
readme: str
24-
license: str
25-
license_file: str
26-
keywords: T.List[str]
27-
categories: T.List[str]
28-
workspace: str
29-
build: str
30-
links: str
31-
include: T.List[str]
32-
exclude: T.List[str]
33-
publish: bool
34-
metadata: T.Dict[str, T.Dict[str, str]]
35-
default_run: str
36-
autolib: bool
37-
autobins: bool
38-
autoexamples: bool
39-
autotests: bool
40-
autobenches: bool
41-
42-
43-
class FixedDependency(TypedDict, total=False):
44-
45-
"""An entry in the *dependencies sections, fixed up."""
46-
47-
version: T.List[str]
48-
registry: str
49-
git: str
50-
branch: str
51-
rev: str
52-
path: str
53-
optional: bool
54-
package: str
55-
default_features: bool
56-
features: T.List[str]
57-
58-
59-
class _BaseFixedBuildTarget(TypedDict, total=False):
60-
path: str
61-
test: bool
62-
doctest: bool
63-
bench: bool
64-
doc: bool
65-
plugin: bool
66-
harness: bool
67-
edition: EDITION
68-
crate_type: T.List[CRATE_TYPE]
69-
required_features: T.List[str]
70-
71-
72-
class FixedBuildTarget(_BaseFixedBuildTarget, total=False):
73-
74-
name: Required[str]
75-
76-
77-
class FixedLibTarget(_BaseFixedBuildTarget, total=False):
78-
79-
name: str
80-
proc_macro: bool

0 commit comments

Comments
 (0)