Commit Graph

50 Commits

Author SHA1 Message Date
Dylan Baker eaf365cb3e decorators: don't unsort sorted values for printing
Currently in our deprecated/new feature printing we carefully sort all
of the values, then put them in a set to print them. Which unsorts them.
I'm assuming this was done because a set looks nice when printed (which
is true). Let's keep the formatting, but print them in a stable order.
2023-01-20 00:18:42 -05:00
Dylan Baker c2e392d78d interpreter: add a feature_validator to KwargInfo
Because sometimes we simply need to open code FeatureNew and
FeatureDeprecated checks, but in a re-usable way.
2022-12-05 15:20:09 -05:00
Eli Schwartz e27653499a add option to typed_kwargs that allows unknown kwargs through
Some functions cannot be fully type checked, because our API allows
fully arbitrary kwargs and treats them as data to pass through to the
underlying feature. For example, hotdoc command line arguments.

This change allows us to type check some kwargs with known types and
possibly required status, and make their values consistent(ly defaultable),
while preserving the optional nature of the additional kwargs.
2022-10-24 15:16:02 +03:00
Dylan Baker 25a3eff797 interpreterbase: fix type annotations for KwargInfo::since_values
These are now allowed to be `Dict[_T | Type[list] | Type[dict], str]`,
but the type annotations weren't updated
2022-08-18 21:57:36 -04:00
Eli Schwartz c1aeb2f7e4
typed_kwargs: support dict/list as "new since" types
Given a kwarg value that is itself a dict/list, we would only check if
the since_values was present within that container. If the since_values
is itself the dict or list type, then we aren't looking for recursive
structures, we just want to know when the function began to accept that
type.

This is relevant in cases where a function accepted a dict, and at one
point began accepting a list (of strings in the form 'key=value'), or
vice versa.
2022-06-17 14:33:01 -04:00
Zbigniew Jędrzejewski-Szmek 8afdecb039 Reword message in warning
"targetting" is verb-derived adjective, which sort-of-works here, but
makes the whole sentence awkward, because there's no verb. Let's just
use present simple.
2022-05-19 07:18:43 -04:00
Zbigniew Jędrzejewski-Szmek 087e7943d0 Reword misleading warning
"tried to use" implies that the attempt was not successful, i.e. that meson
ignored the feature. But that is not what happens, apart from the warning the
feature works just fine. The new message is also shorter ;)
2022-05-19 07:18:43 -04:00
Eli Schwartz c231c4bd2a
move a bunch of imports into TYPE_CHECKING blocks
These are only used for type checking, so don't bother importing them at
runtime.

Generally add future annotations at the same time, to make sure that
existing uses of these imports don't need to be quoted.
2022-03-07 19:09:50 -05:00
Eli Schwartz a009eacc65
treewide: string-quote the first argument to T.cast
Using future annotations, type annotations become strings at runtime and
don't impact performance. This is not possible to do with T.cast though,
because it is a function argument instead of an annotation.

Quote the type argument everywhere in order to have the same effect as
future annotations. This also allows linters to better detect in some
cases that a given import is typing-only.
2022-03-07 19:01:04 -05:00
Eli Schwartz 187dc656f4
merge various TYPE_CHECKING blocks into one
A bunch of files have several T.TYPE_CHECKING blocks that each do some
things which could just as well be done once, with a single `if`
statement. Make them do so.
2022-03-07 19:01:04 -05:00
Eli Schwartz d39b330075 clean up FeatureCheck signature to move location to use time
The point of a .use() function is because we don't always have the
information we need to use a feature check, so we allow creating the
feature and then storing it for later use. When implementing location
checks, although it is optional, actually using it violated that design.

Move the location out of the init method for FeatureCheck itself. It
remains compatible with all cases of .single_use(), but fix the rest up.
2022-03-01 12:13:24 -08:00
Eli Schwartz 2b04e8c5e5 remove useless kwarg that was never used
I am not sure why it ever got added. It looks superficially similar to
FeatureCheck itself, but doesn't need to.
2022-03-01 12:13:24 -08:00
Eli Schwartz c0b8e02d9f
FeatureNew: add mypy type annotations for subproject arg
Use a derived type when passing `subproject` around, so that mypy knows
it's actually a SubProject, not a str. This means that passing anything
other than a handle to the interpreter state's subproject attribute
becomes a type violation, specifically when the order of the *four*
different str arguments is typoed.
2022-02-14 20:40:41 -05:00
Eli Schwartz 4b351aef26
first pass at migrating to dataclasses
In some cases, init variables that accept None as a sentinel and
immediately overwrite with [], are migrated to dataclass field
factories. \o/

Note: dataclasses by default cannot provide eq methods, as they then
become unhashable. In the future we may wish to opt into declaring them
frozen, instead/additionally.
2022-01-10 18:36:57 -05:00
Dylan Baker 283c65578d decorators: fold some duplicated code into a closure 2021-12-06 20:06:14 -05:00
Dylan Baker f5d961f127 add message option to since_values and deprecated_values
This allows these two arguments to take a tuple of (version, message),
where the message will be passed to Feature*'s message parameter
2021-12-06 20:06:14 -05:00
Dylan Baker 930cbdb86d Add deprecated_message and since_message to KwargInfo
For passing an extra message to Feature*

This allows providing a more detailed message.
2021-12-06 20:06:14 -05:00
Dylan Baker 82136da933 interpreterbase/decorators: Fix types of deprecated_values and since_values
Which shouldn't be Dict[str, str], they should be Dict[_T, str], as nay
value that can be passed to types is valid here.
2021-12-06 20:06:14 -05:00
Eli Schwartz a21c7d54fb
fix stray typo 2021-12-05 11:39:20 -05:00
Eli Schwartz 4b2c54569d
clean up function signatures in preparation for dataclasses
FeatureCheck always immediately sets extra_message to '' if it isn't
explicitly passed, so there is really no point in using None as a
sentinel that is never used.

Names used in init functions are sometimes pointlessly different from
the class instance attributes they are immediately assigned to. They
would make more sense if defined properly.
2021-12-05 11:39:20 -05:00
Dylan Baker 762c504612 typed_kwargs: use | for type unions, not ,
Python uses this syntax now, as does typescript and other languages
2021-11-22 12:24:33 -08:00
Dylan Baker ecc47d67a9 typed_kwargs: provide better error messages for wrong container types
Currently, if you pass a `[]string`, but the argument expects
`[]number`, then you get a message like `expected list[str] but got
list`. That isn't helpful. With this patch arrays and dictionaries will
both print messages with the types provided.
2021-11-22 12:24:14 -08:00
Dylan Baker 1f6351461c typed_kwargs: move some closures around to increase code clarity
The inner closure of the typed_kwargs function is already complicated
enough without defining closures in the middle of a loop. Let's just
pass the types_tuple as an argument to both avoid redefining the
function over and over, and also make the whole thing easier to read.
2021-11-22 11:07:36 -08:00
Eli Schwartz fae7a70292
FeatureDeprecated: add a notice summary of future deprecations
Since these aren't warnings, per se, we don't note every single call
site that has one. And we raise mlog.notice in non-fatal mode to avoid
either:
- being too threatening
- making builds fail with --fatal-meson-warnings

Nevertheless, it is useful to give people a heads-up that there is an
upgrade opportunity, rather than waiting until they upgrade and then
causing projects to begin printing fatal warnings.
2021-11-20 20:48:30 -05:00
Eli Schwartz 8dbb0ee476
Feature kwargs decorator: automatically report the nodes which trigger an issue 2021-11-20 20:48:30 -05:00
Eli Schwartz 7a033c7887
add location support to feature checks
mlog can already print location info, and we use this often -- including
for custom feature warnings already. Make this work everywhere, so that
it is feasible to move such custom warnings to globally tracked
Features.
2021-11-20 20:48:29 -05:00
Eli Schwartz 384151c541 migrate python 3.5 compatible superclass variable annotations to 3.6
As we now require python 3.6, we can declare their types without
initializing them.
2021-11-01 00:46:51 +02:00
Eli Schwartz 8947352889 fix various flake8 whitespace errors 2021-10-27 09:51:52 -04:00
Christian Clauss a5020857f3 Fix typos discovered by codespell 2021-10-10 16:12:25 -04:00
Xavier Claessens 709d151eb9 typed_kwargs: Fix when ContainerTypeInfo is used in a tuple
info.types could be a tuple like (str, ContainerTypeInfo()). That means
we have to check types one by one and only print error if none of them
matched.

Also fix the case when default value is None for a container type, it
should leave the value to None to be able to distinguish between unset
and empty list.
2021-10-09 18:13:34 -04:00
Eli Schwartz 4ab70c5512
fix extra whitespace
discovered via flake8 --select E303
2021-10-04 16:29:31 -04:00
Dylan Baker 7e43432a0c interpreterbase/decorators: KwargIfno validator must take an relaxed type
I've used any because it needs to be infinitely recursive, something
that we simply can't model. But basically until it goes into validator
we have no way of knowing what's going on, since one can write code
like:

```python
KwargInfo[str]('arg', object, validator=_some_very_complex_logic_to_get_specific_string)
```

As such, we can't assume that validator is receiving a type _T, it could
be anything.
2021-09-30 21:01:38 +02:00
Daniel Mensinger 6b00c7dc81 Remove helpers.check_stringlist() 2021-09-25 12:44:11 +02:00
Daniel Mensinger 43302d3296 interpreter: Introduce BooleanHolder for the bool primitive 2021-09-01 19:17:01 +02:00
Daniel Mensinger 8d92e6d865 interpreter: Add IntegerHolder 2021-08-31 23:01:21 +02:00
Daniel Mensinger 86f70c873a interpreter: Introduce operators support for InterpreterObjects 2021-08-31 23:01:21 +02:00
Dylan Baker 4d7031437c pylint: turn on superflous-parens
We have a lot of these. Some of them are harmless, if unidiomatic, such
as `if (condition)`, others are potentially dangerous `assert(...)`, as
`assert(condtion)` works as expected, but `assert(condition, message)`
will result in an assertion that never triggers, as what you're actually
asserting is `bool(tuple[2])`, which will always be true.
2021-08-31 16:28:54 -04:00
Xavier Claessens 88a1bed81b decorators: Make unknown kwarg fatal 2021-08-30 14:00:54 -04:00
Xavier Claessens f8cfd91d71 Simplify get_callee_args 2021-08-30 14:00:54 -04:00
Dylan Baker ce392acad4 interpreterbase: ensure that the default vaule to KwargInfo is a valid type
Because currently you can write something like:
```python
KwargInfo('capture', bool)
```
Which proclaims "this must be bool", but the default is then not valid.
2021-08-27 14:54:30 -07:00
Dylan Baker d8d09138c7 interpreter: allow KwargInfo.evolve to change the name as well 2021-08-16 16:21:51 -07:00
Dylan Baker 386b312fa9 interpreterbase/decorators: fix typed_kwargs return type
Because of the convertor function we have no guarantee that what we're
getting is in fact a `Dict[str, TYPE_var]`, it might well be anything in
the values, so we need to do some casting and set the return type to
object. This works out fine in practice as our declared `TypeDict`
inputs in the actual function signatures will be used instead.
2021-08-04 19:09:08 -04:00
Daniel Mensinger 971a0b1775 fix: get_variable default variables are not ObjectHolders (fixes #8936) 2021-07-02 17:14:44 +03:00
Daniel Mensinger 8f7343831b refactor: Refactor BothLibraries logic
This commit introduces a new type of `HoldableObject`: The
`SecondLevelHolder`. The primary purpose of this class is
to handle cases where two (or more) `HoldableObject`s are
stored at the same time (with one default object). The
best (and currently only) example here is the `BothLibraries`
class.
2021-06-26 12:49:35 +02:00
Dylan Baker dd296f321b interpreterbase: Add evolve to KwargInfo
This works just like OptionKey.evolve, pass new keyword arguments to
override old ones, otherwise the original versions are copied to the new
one.
2021-06-22 09:12:54 -07:00
Daniel Mensinger c2c7f7c9d7 holders: Ensure that InterpreterBase is the sole instance for (un)holderifying 2021-06-18 23:48:33 +02:00
Daniel Mensinger 8f997efc8b decorators: Add not_set_warning to KwargInfo 2021-06-18 23:48:33 +02:00
Daniel Mensinger bad14b23d2 decorators: Fix typo 2021-06-18 23:48:33 +02:00
Dylan Baker 5bb75dc3af interpreterbase: Add deprecated_values and since_values to KwargInfo
This allows checking specific values that are added or deprecated, which
we do a surprising amount of. This works with both containers and scalar
values
2021-06-14 12:30:02 -07:00
Daniel Mensinger e686545fce interpreter: Split decorators from interpreterbase.py 2021-06-11 10:42:18 +02:00