Commit Graph

105 Commits

Author SHA1 Message Date
Josh Soref cf9fd56bc9 fix various spelling issues
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2023-04-11 19:21:05 -04:00
Eli Schwartz 8fc4649bfe
propagate the most accurate node to error messages
During evaluation of codeblocks, we start off with an iteration of
nodes, and then while evaluating them we may update the global
self.current_node context. When catching and formatting errors, we
didn't take into account that the node might be updated from the
original top-level iteration.

Switch to formatting errors using self.current_node instead, to ensure
we can point at the likely most-accurate actual cause of an error.

Also update the current node in a few more places, so that function
calls always see the function call as the current node, even if the most
recently parsed node was an argument to the function call.

Fixes #11643
2023-04-04 15:06:17 -04:00
Eli Schwartz c91a6ad013 re-deduplicate feature warnings printed at the end of setup
In commit eaf365cb3e we explicitly sorted
them for neatness, with the rationale that we were restoring intentional
behavior and we only need a set for stylistic purposes.

This actually wasn't true, because we never sorted them to begin with
(we did sort the version numbers), but sorting them is fine. The bigger
issue is that we actually used a set to avoid printing the same feature
type multiple times. Now we do print them multiple times -- because each
registered feature includes the unique node.

Fix this by using both sorted and a set.

Fix tests that should in retrospect have flagged this as an issue, but
were added later on in the same series to check something else entirely,
happen to cover this too, and were presumably copied directly from
stdout as-is...
2023-03-09 22:04:38 -05:00
Xavier Claessens 93c11f2494 typed_kwargs: Remove feature_validator as it's not currently used 2023-03-04 14:19:27 -05:00
Xavier Claessens 0f67913dee typed_kwargs: Extend since_values and deprecated_values for types 2023-03-04 14:19:27 -05:00
Eli Schwartz 314382d6ff
handle meson_version after parsing but before invalid project() kwargs
If we add new kwargs to a function invoked on the first line, we also
need to validate the meson_version before erroring out due to unknown
kwargs. Even if the AST was successfully built.

Amusingly, we also get to improve the error message a bit. By passing
the AST node instead of an interpreter node, we get not just line
numbers, but also column offsets of the issueful meson_version. That
broke the stdout of another failing test; adapt it.
2023-03-01 23:36:31 -05:00
Eli Schwartz 878c1604e6
handle meson_version even when the build file fails to parse
If the meson.build file is sufficiently "broken", even attempting to lex
and parse it will totally fail, and we error out without getting the
opportunity to evalaute the project() function. This can fairly easily
happen if we add new grammar to the syntax, which old versions of meson
cannot understand. Setting a minimum meson_version doesn't help, because
people with a too-old version of meson get parser errors instead of
advice about upgrading meson.

Examples of this include adding dict support to meson.

There are two general approaches to solving this issue, one of which
projects are empowered to do:

- refactor the project to place too-new syntax in a subdir() loaded
  build file, so the root file can be interpreted

- teach meson to catch errors in building the initial AST, and just load
  enough of the AST to check for meson_version advice

This implements the latter, allowing to future-proof the build
grammar.
2023-03-01 23:30:49 -05:00
Xavier Claessens f0dc61a764 interpreter: Add testcase..endtestcase clause support
This is currently only enabled when running unit tests to facilitate
writing failing unit tests.

Fixes: #11394
2023-03-01 20:13:34 -05:00
Charles Brunet 5dbe49aa33 prevent unhandled exception for operations on None
For instance, when writing `-subdir('a')` or `not subdir('a')`.

Fixes #11225.
2023-02-27 13:40:16 -05:00
Eli Schwartz a21af43200
micro-optimize: define typing-only objects in TYPE_CHECKING
Union types that exist solely for use as annotations don't need to be
created in normal runs.
2023-02-01 17:01:30 -05:00
Eli Schwartz 680b5ff819
treewide: add future annotations import 2023-02-01 17:01:30 -05:00
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
Dylan Baker a5d547e0d9
pylint: enable useless-return 2022-11-29 23:26:05 -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
Eli Schwartz 78bf1ec56e
fix obscure crash on unbound variable
This can be triggered if someone tries to call a non-ID. The example
reproducer was:

```
if (var = dependency(...)).found()
```

This produced a traceback ending in

```
    raise InvalidArguments(f'Variable "{object_name}" is not callable.')
UnboundLocalError: local variable 'object_name' referenced before assignment
```

After this commit, the error is reported as:

```
ERROR: AssignmentNode is not callable.
```
2022-08-30 15:30:55 -04: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
Eli Schwartz 1c52ac4e15
move various imports into TYPE_CHECKING blocks for neatness 2022-05-23 16:44:08 -04:00
Eli Schwartz e5c7dc199a
typing: use forward reference for types defined later in file 2022-05-23 16:44:07 -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
Peter Lesslie d771fc7d0b Add support for multiline f-strings
+ Extend the parser to recognize the multiline f-strings, which the
documentation already implies will work.

The syntax is like:
```
x = 'hello'
y = 'world'

msg = f'''This is a multiline string.

Sending a message: '@x@ @y@'
'''
```

which produces:
```
This is a multiline string.

Sending a message: 'hello world'

```

+ Added some f-string tests cases to "62 string arithmetic" to exercise
the new behavior.
2022-05-01 12:47:37 -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 fec7265b5c
flake8: use plain strings instead of f-strings when no variables used 2022-02-16 18:19:12 -05: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 3990dc6d64 flake8: fix indentation style 2022-01-27 10:48:01 -08: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 038b31e72b
various manual conversion of percent-formatted strings to f-strings 2021-11-01 20:26:18 -04: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 f4b91c4306 Revert "mark a couple of typing-only imports as noqa, to appease pyflakes"
This reverts commit 6cc1b8441c.

The latest version of pyflakes learned to detect that correctly.
2021-10-27 09:51:52 -04:00
Eli Schwartz 8947352889 fix various flake8 whitespace errors 2021-10-27 09:51:52 -04:00
Daniel Mensinger 2d8da3cb11 interpreter: Fix missing featuer check (fixes #9425) 2021-10-24 09:58:20 -04:00
Christian Clauss a5020857f3 Fix typos discovered by codespell 2021-10-10 16:12:25 -04:00