Commit Graph

13 Commits

Author SHA1 Message Date
Fraser Waters 19f67eb163
Add 'typechecker' option to python runtime ()
<!--- 
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->

Fixes https://github.com/pulumi/pulumi/issues/15724.

This adds a new runtime option to the python language host `typeChecker`
which can be set to either "mypy" or "pyright". If it's set the language
host will run the mypy/pyright module before running the program.

## Checklist

- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
  - [x] I have formatted my code using `gofumpt`

<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [x] I have added tests that prove my fix is effective or that my
feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2024-03-28 10:41:22 +00:00
Justin Van Patten fd7a9a88c0
[tests/integration] Remove `pulumi` from `requirements.txt` ()
In preparation for supporting Python 3.12...

These tests have a `requirements.txt` file that depends on the released
version of the `pulumi` package, but that package depends on the old
`grpcio` package that does not work Python 3.12.

Note that `ProgramTest` first installs `requirements.txt` in the virtual
environment it creates, then it installs any local dependencies
specified via `ProgramTestOptions.Dependencies`.

To make these tests work on Python 3.12, remove `pulumi` from
`requirements.txt`. After we publish a new `pulumi` package that
supports Python 3.12, we could consider reverting this change.

Aside: I considered changing `ProgramTest` to install
`ProgramTestOptions.Dependencies` deps _before_ `requirements.txt` deps.
That would avoid the need to change these `requirements.txt` files and
would work for the most part. If the version specified in
`requirements.txt` is satisfied by the already installed local version,
then pip doesn't install the version from `requirements.txt`. Where this
doesn't work is if `requirements.txt` specifies an older version range.
Then the local dependency would be uninstalled and the older version
from `requirements.txt` would be installed. Which isn't the behavior we
want. And there's very likely `requirements.txt` files in other repos in
this situation (there were even some in this PR that depend on v2.x!)
and I didn't want to change the behavior for those. We'd have to make it
an opt-in `ProgramTestOptions` option. It seemed simpler to just modify
these `requirements.txt` files.

Note: The change to
`tests/integration/python/stack_truncate/main_dir_specified/bar/requirements.txt`
depends on https://github.com/pulumi/pulumi/pull/15225. It's going to
fail without that change.
2024-01-24 14:16:40 +00:00
Justin Van Patten e9ac6f3c9c
[tests/integration] Fix `resource_args` for Python 3.12 ()
In preparation for supporting Python 3.12...

This test is generating a local example library using SDKgen and then
running a program that uses it.

When running on Python 3.12, without this fix, we get an error from the
generated example library's `_utilities.py` file:

```
ModuleNotFoundError: No module named 'pkg_resources'
```

This is because `_utilities.py` depends on `pkg_resources` from
`setuptools`, but our generated provider SDKs do not specify they have a
dependency on `setuptools`. The problem on Python 3.12 is because
virtual environments created with `python -m venv` no longer include
`setuptools` in the virtual environment
(https://github.com/python/cpython/issues/95299).

This generally isn't a problem with projects created using `pulumi new`
because we will explicitly install `setuptools` in the created virtual
environment. But it is a problem in this case, because `ProgramTest` is
creating the virtual environment, and it doesn't install setuptools in
it.

To workaround, for now, include `setuptools` in the test program's
`requirements.txt`.

When we fix Python SDKgen to no longer use `pkg_resources`, we can
remove `setuptools` from the test program's `requirements.txt`
(https://github.com/pulumi/pulumi/issues/12414).

Also remove the `pulumi` dependency in `requirements.txt` as it the
currently published package can't be installed on Python 3.12 due to the
dependency on `grpcio` that doesn't work on Python 3.12. The test
installs the locally built Python SDK, so it's not needed in
`requirements.txt` anyway.

Aside: This test really should be a runtime SDKgen test, which wouldn't
have this problem because it uses the CLI's code for creating the
virtual environment, which installs `setuptools`.

Also note: While looking at this test, I cleaned up a part of the test
that was replacing `${VERSION}` in the generated library's `setup.py`,
which is no longer necessary because that's not how the version is
replaced anymore. A default placeholder version is included that will
work as-is.
2024-01-24 12:47:34 +00:00
Kyle Pitzen 7b21b5db66
[sdk/python] Adds a default exception when dependency cycles are created ()
<!--- 
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->

Currently, when we detect that we've created a cycle in the dependency
graph, we early-exit. This works well enough for simple cycles, but when
early exiting is not sufficient (as when providing a resource output as
an argument to another resource's inputs), we will still fail to resolve
the full dependency graph. This PR introduces an exception-by-default,
as any cycles represent an unsafe/invalid dependency graph and should be
resolved manually. We also provide an escape hatch to fall back to
current behavior, in case users would prefer to retain the ability to
create unsafe dependency graphs (potentially introducing infinite hangs
when resolving those graphs).

Fixes https://github.com/pulumi/pulumi/issues/13551

## Checklist

- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
  - [ ] I have formatted my code using `gofumpt`

<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [x] I have added tests that prove my fix is effective or that my
feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @Pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
2023-11-21 16:26:02 +00:00
Fraser Waters 04ce92c59f Test duplicate outputs
Regression test to cover https://github.com/pulumi/pulumi/issues/9411
2023-06-29 10:46:37 +01:00
stefins 80517bb301
fix: handle exception for main not found in python ()
Improve error message when pulumi-python cannot find a main program.
2022-09-07 08:42:38 -07:00
Kyle Dixler ca8642f34a
[sdk/python] minimize internal pulumi stacks in python stacktrace ()
This PR removes internal pulumi stack traces from the traceback output when a user's python pulumi program raises an Exception.
2022-08-18 07:46:04 -07:00
Anton Tayanovskyy 493bac4c18
Make virtualenv paths relative to root when main points elsewhere ()
* Propagate workspace.Project metadata to plugin init

* Get to a working fix

* Propagate Root via plugin context

* Propagate root instead of yaml path

* Revert out unnecessary parameter propagation

* Root is now always absolute at this point; simplify code and docs

* Drop python conditional and propagate unused -root to all lang hosts

* Add tests that fail before and pass after

* Lint

* Add changelog entry
2021-05-14 13:41:55 -04:00
Paul Stack e955a6b06a Refactor Mock newResource and call to accept property bag rather than individual args () 2021-04-14 19:32:18 +01:00
Justin Van Patten 1112c513c0 [sdk/python] Improved dict key translation support ()
This change addresses Python dictionary key translation issues. When the
type of `props` passed to the resource is decorated with `@input_type`,
the type's and resource's property name metadata will be used for dict
key translations instead of the resource's `translate_input_property`
and `translate_output_property` methods.

The generated provider SDKs will be updated to opt-in to this new
behavior:

- FIX: Keys in user-defined dicts will no longer be unintentionally
  translated/modified.

- BREAKING: Dictionary keys in nested output classes are now
  consistently snake_case. If accessing camelCase keys from such output
  classes, move to accessing the values via the snake_case property
  getters (or snake_case keys). Generated SDKs will log a warning
  when accessing camelCase keys.

When serializing inputs:

  - If a value is a dict and the associated type is an input type, the
    dict's keys will be translated based on the input type's property
    name metadata.

  - If a value is a dict and the associated type is a dict (or Mapping),
    the dict's keys will _not_ be translated.

When resolving outputs:

  - If a value is a dict and the associated type is an output type, the
    dict's keys will be translated based on the output type's property
    name metadata.

  - If a value is a dict and the associated type is a dict (or Mapping),
    the dict's keys will _not_ be translated.
2021-04-14 19:32:18 +01:00
Justin Van Patten dcf4359c57
[codegen/python] Support `<Resource>Args` classes ()
Add support for creating instances of resources in Python using a
`<Resource>Args` class. This capability aligns with how args are passed
to resources in all the other language SDKs and the separate object bag
allows the properties to be manipulated/validated/passed-around before
creating the resource.
2021-04-02 10:09:17 -07:00
Anton Tayanovskyy 4e5828a890
Avoid double-quailfying venv folder path ()
* Avoid double-quailfying venv folder path

* Replace `path` with `filepath`

* Add a Python integration test to cover venv auto-creation

* Merged

* Fix spelling

Co-authored-by: Justin Van Patten <jvp@justinvp.com>

* Make AbsPath and RelPath test variants

* Fix issue on Windows backslash paths

* Debug windows test failure: more logging and aggressive YAML escaping

* Use filepath.IsAbs instead of path.IsAbs

Co-authored-by: Justin Van Patten <jvp@justinvp.com>
2021-03-24 15:51:46 -04:00
Justin Van Patten 9b0169be35
Fix pylint(no-member) when accessing resource.id ()
Pylint currently reports `E1101: Instance of 'Bucket' has no 'id' member (no-member)` on lines in Pulumi Python programs like:

```python
pulumi.export('bucket_name', bucket.id)
```

Here's a description of this message from http://pylint-messages.wikidot.com/messages:e1101:

> Used when an object (variable, function, …) is accessed for a non-existent member.
>
> False positives: This message may report object members that are created dynamically, but exist at the time they are accessed.

This appears to be a false positive case: `id` isn't set in the constructor (it's set later in `register_resource`) and Pylint isn't able to figure this out statically. `urn` has the same problem. (Oddly, Pylint doesn't complain when accessing other resource output properties).

This change refactors `register_resource` so that `id` and `urn` can be assigned in the resource's constructor, so that Pylint can see it being assigned. The change also does the same with `read_resource`.
2020-06-12 12:41:56 -07:00