26
26
if T .TYPE_CHECKING :
27
27
from typing_extensions import Protocol , Self
28
28
29
- from . import manifest
29
+ from . import manifest , raw
30
30
from .. import mparser
31
31
from ..environment import Environment
32
32
from ..interpreterbase import SubProject
@@ -40,6 +40,8 @@ class DataclassInstance(Protocol):
40
40
manifest .FixedDependency , manifest .FixedLibTarget ,
41
41
manifest .FixedBuildTarget )
42
42
43
+ _R = T .TypeVar ('_R' , bound = 'raw._BaseBuildTarget' )
44
+
43
45
44
46
_EXTRA_KEYS_WARNING = (
45
47
"This may (unlikely) be an error in the cargo manifest, or may be a missing "
@@ -61,15 +63,15 @@ def fixup_meson_varname(name: str) -> str:
61
63
62
64
# Pylance can figure out that these do not, in fact, overlap, but mypy can't
63
65
@T .overload
64
- def _fixup_raw_mappings (d : manifest .BuildTarget ) -> manifest .FixedBuildTarget : ... # type: ignore
66
+ def _fixup_raw_mappings (d : raw .BuildTarget ) -> manifest .FixedBuildTarget : ... # type: ignore
65
67
66
68
@T .overload
67
- def _fixup_raw_mappings (d : manifest .LibTarget ) -> manifest .FixedLibTarget : ... # type: ignore
69
+ def _fixup_raw_mappings (d : raw .LibTarget ) -> manifest .FixedLibTarget : ... # type: ignore
68
70
69
71
@T .overload
70
- def _fixup_raw_mappings (d : manifest .Dependency ) -> manifest .FixedDependency : ...
72
+ def _fixup_raw_mappings (d : raw .Dependency ) -> manifest .FixedDependency : ...
71
73
72
- def _fixup_raw_mappings (d : T .Union [manifest .BuildTarget , manifest .LibTarget , manifest .Dependency ]
74
+ def _fixup_raw_mappings (d : T .Union [raw .BuildTarget , raw .LibTarget , raw .Dependency ]
73
75
) -> T .Union [manifest .FixedBuildTarget , manifest .FixedLibTarget ,
74
76
manifest .FixedDependency ]:
75
77
"""Fixup raw cargo mappings to ones more suitable for python to consume.
@@ -153,7 +155,7 @@ def __post_init__(self) -> None:
153
155
self .api = _version_to_api (self .version )
154
156
155
157
@classmethod
156
- def from_raw (cls , raw : manifest .Package ) -> Self :
158
+ def from_raw (cls , raw : raw .Package ) -> Self :
157
159
pkg = T .cast ('manifest.FixedPackage' ,
158
160
{fixup_meson_varname (k ): v for k , v in raw .items ()})
159
161
pkg = _handle_unknown_keys (pkg , cls , f'Package entry { pkg ["name" ]} ' )
@@ -233,7 +235,7 @@ def __post_init__(self, name: str) -> None:
233
235
raise MesonException (f'Cannot determine minimum API version from { self .version } .' )
234
236
235
237
@classmethod
236
- def from_raw (cls , name : str , raw : manifest .DependencyV ) -> Dependency :
238
+ def from_raw (cls , name : str , raw : raw .DependencyV ) -> Dependency :
237
239
"""Create a dependency from a raw cargo dictionary"""
238
240
if isinstance (raw , str ):
239
241
return cls (name , version .convert (raw ))
@@ -242,7 +244,7 @@ def from_raw(cls, name: str, raw: manifest.DependencyV) -> Dependency:
242
244
243
245
244
246
@dataclasses .dataclass
245
- class BuildTarget :
247
+ class BuildTarget ( T . Generic [ _R ]) :
246
248
247
249
name : str
248
250
crate_type : T .List [manifest .CRATE_TYPE ] = dataclasses .field (default_factory = lambda : ['lib' ])
@@ -270,13 +272,13 @@ class BuildTarget:
270
272
plugin : bool = False
271
273
272
274
@classmethod
273
- def from_raw (cls , raw : manifest .BuildTarget ) -> Self :
275
+ def from_raw (cls , raw : raw .BuildTarget ) -> Self :
274
276
name = raw .get ('name' , '<anonymous>' )
275
277
build = _handle_unknown_keys (_fixup_raw_mappings (raw ), cls , f'Binary entry { name } ' )
276
278
return cls (** build )
277
279
278
280
@dataclasses .dataclass
279
- class Library (BuildTarget ):
281
+ class Library (BuildTarget [ 'raw.LibTarget' ] ):
280
282
281
283
"""Representation of a Cargo Library Entry."""
282
284
@@ -288,44 +290,44 @@ class Library(BuildTarget):
288
290
doc_scrape_examples : bool = True
289
291
290
292
@classmethod
291
- def from_raw (cls , raw : manifest .LibTarget , fallback_name : str ) -> Self : # type: ignore[override]
293
+ def from_raw (cls , raw : raw .LibTarget , fallback_name : str ) -> Self : # type: ignore[override]
294
+ if 'name' not in raw :
295
+ raw ['name' ] = fallback_name
292
296
fixed = _fixup_raw_mappings (raw )
293
297
294
298
# We need to set the name field if it's not set manually, including if
295
299
# other fields are set in the lib section
296
- if 'name' not in fixed :
297
- fixed ['name' ] = fallback_name
298
300
fixed = _handle_unknown_keys (fixed , cls , f'Library entry { fixed ["name" ]} ' )
299
301
300
302
return cls (** fixed )
301
303
302
304
303
305
@dataclasses .dataclass
304
- class Binary (BuildTarget ):
306
+ class Binary (BuildTarget [ 'raw.BuildTarget' ] ):
305
307
306
308
"""Representation of a Cargo Bin Entry."""
307
309
308
310
doc : bool = True
309
311
310
312
311
313
@dataclasses .dataclass
312
- class Test (BuildTarget ):
314
+ class Test (BuildTarget [ 'raw.BuildTarget' ] ):
313
315
314
316
"""Representation of a Cargo Test Entry."""
315
317
316
318
bench : bool = True
317
319
318
320
319
321
@dataclasses .dataclass
320
- class Benchmark (BuildTarget ):
322
+ class Benchmark (BuildTarget [ 'raw.BuildTarget' ] ):
321
323
322
324
"""Representation of a Cargo Benchmark Entry."""
323
325
324
326
test : bool = True
325
327
326
328
327
329
@dataclasses .dataclass
328
- class Example (BuildTarget ):
330
+ class Example (BuildTarget [ 'raw.BuildTarget' ] ):
329
331
330
332
"""Representation of a Cargo Example Entry."""
331
333
@@ -365,7 +367,7 @@ def __post_init__(self) -> None:
365
367
self .system_dependencies = {k : SystemDependency .from_raw (k , v ) for k , v in self .package .metadata .get ('system-deps' , {}).items ()}
366
368
367
369
368
- def _convert_manifest (raw_manifest : manifest .Manifest , subdir : str , path : str = '' ) -> Manifest :
370
+ def _convert_manifest (raw_manifest : raw .Manifest , subdir : str , path : str = '' ) -> Manifest :
369
371
return Manifest (
370
372
Package .from_raw (raw_manifest ['package' ]),
371
373
{k : Dependency .from_raw (k , v ) for k , v in raw_manifest .get ('dependencies' , {}).items ()},
@@ -503,9 +505,9 @@ def _load_manifest(self, subdir: str) -> Manifest:
503
505
manifest_ = self .manifests .get (subdir )
504
506
if not manifest_ :
505
507
filename = os .path .join (self .environment .source_dir , subdir , 'Cargo.toml' )
506
- raw = load_toml (filename )
507
- if 'package' in raw :
508
- raw_manifest = T .cast ('manifest .Manifest' , raw )
508
+ toml = load_toml (filename )
509
+ if 'package' in toml :
510
+ raw_manifest = T .cast ('raw .Manifest' , toml )
509
511
manifest_ = _convert_manifest (raw_manifest , subdir )
510
512
self .manifests [subdir ] = manifest_
511
513
else :
@@ -821,7 +823,7 @@ def load_wraps(source_dir: str, subproject_dir: str) -> T.List[PackageDefinition
821
823
filename = os .path .join (source_dir , 'Cargo.lock' )
822
824
if os .path .exists (filename ):
823
825
try :
824
- cargolock = T .cast ('manifest .CargoLock' , load_toml (filename ))
826
+ cargolock = T .cast ('raw .CargoLock' , load_toml (filename ))
825
827
except TomlImplementationMissing as e :
826
828
mlog .warning ('Failed to load Cargo.lock:' , str (e ), fatal = False )
827
829
return wraps
0 commit comments