Skip to content

Commit d78275f

Browse files
committed
Simplify Options definition
1 parent 61c1066 commit d78275f

1 file changed

Lines changed: 13 additions & 17 deletions

File tree

‎bfg9000/options.py‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import enum
22
import sys
3+
import typing
34
from collections import namedtuple
45
from inspect import Signature, Parameter
56

6-
from . import path, safe_str
7+
from . import safe_str
78
from .iterutils import isiterable
89
from .file_types import *
910
from .packages import Framework
11+
from .path import BasePath
1012

1113

1214
class option_list:
@@ -127,14 +129,6 @@ def _get_ns_annotations(attrs):
127129
return {}
128130

129131

130-
def _get_annotations(thing):
131-
typ = thing if isinstance(thing, type) else type(thing)
132-
if sys.version_info >= (3, 14):
133-
import annotationlib
134-
return annotationlib.get_annotations(typ)
135-
return typ.__dict__.get('__annotations__', {})
136-
137-
138132
class OptionMeta(type):
139133
@staticmethod
140134
def __make_parameters(fields, defaults):
@@ -144,11 +138,13 @@ def __make_parameters(fields, defaults):
144138
if isinstance(v, variadic):
145139
has_variadic = True
146140
kind = Parameter.VAR_POSITIONAL
141+
annotation = typing.Tuple[v.type]
147142
else:
148143
kind = Parameter.POSITIONAL_OR_KEYWORD
144+
annotation = v
149145

150146
default = defaults.get(k, Parameter.empty)
151-
yield Parameter(k, kind, default=default)
147+
yield Parameter(k, kind, default=default, annotation=annotation)
152148

153149
def __new__(cls, name, bases, attrs):
154150
annotations = _get_ns_annotations(attrs)
@@ -195,13 +191,13 @@ def __init__(self, *args, **kwargs):
195191
bound = self._signature.bind(*args, **kwargs)
196192
bound.apply_defaults()
197193

198-
annotations = _get_annotations(self)
199194
for name, value in bound.arguments.items():
200-
typ = annotations[name]
201-
if isinstance(typ, variadic):
202-
value = [self.__check_type(typ.type, i) for i in value]
195+
param = self._signature.parameters[name]
196+
if param.kind == Parameter.VAR_POSITIONAL:
197+
annotation = typing.get_args(param.annotation)[0]
198+
value = [self.__check_type(annotation, i) for i in value]
203199
else:
204-
value = self.__check_type(typ, value)
200+
value = self.__check_type(param.annotation, value)
205201
setattr(self, name, value)
206202

207203
@classmethod
@@ -267,7 +263,7 @@ class define(Option):
267263
lib_dir = option('lib_dir', directory=Directory)
268264
lib_literal = option('lib_literal', value=safe_str.stringy_types)
269265
module_def = option('module_def', value=ModuleDefFile)
270-
rpath_link_dir = option('rpath_link_dir', path=path.BasePath)
266+
rpath_link_dir = option('rpath_link_dir', path=BasePath)
271267

272268

273269
@Option.alias
@@ -286,7 +282,7 @@ class RpathWhen(OptionFlag):
286282

287283

288284
class rpath_dir(Option):
289-
path: path.BasePath
285+
path: BasePath
290286
when: RpathWhen = RpathWhen.always
291287

292288

0 commit comments

Comments
 (0)