Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(python): classproperty not working with type checkers #3694

Merged
merged 15 commits into from
Aug 11, 2022

Conversation

RomainMuller
Copy link
Contributor

@RomainMuller RomainMuller commented Aug 2, 2022

The jsii.python submodule was not re-exported in __init__.py, which
made type resolves not manage to access the. @classproperty decorator
and default to treating it as Any.

This adds some missing type annotations, runs pyright on the runtime
library and tests thereof (as mypy did not identify this problem),
and adds the missing export declaration.

Fixes #3633


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

In some scenarios, the `@classproperty` decorator would not yield
something that python type checkers would consider as a property. This
attempts to add additional type hints to help type checkers discover
that the returned object is a property descriptor.

Fixes #3633
@RomainMuller RomainMuller requested a review from a team August 2, 2022 09:57
@RomainMuller RomainMuller self-assigned this Aug 2, 2022
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Aug 2, 2022
Copy link
Contributor

@comcalvi comcalvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I just have a few questions before I feel comfortable merging this in

@@ -164,7 +166,7 @@ def new_combined_struct(structs: Iterable[Type]) -> Type:
label = " + ".join(struct.__name__ for struct in structs)

def __init__(self, **kwargs):
self._values: Mapping[str, Any] = kwargs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove the type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to pyright (which is correct), this is not a valid location to have a type signature at...

Comment on lines -155 to +157
return super.__setattr__(self, name, value)
return super().__setattr__(name, value)
Copy link
Contributor

@comcalvi comcalvi Aug 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to be a harmless cosmetic change, but I'm curious; why make this change in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type signature of the previous chain would not allow pyright to determine the signature of __setattr__ and it complained about receiving 3 arguments when 2 were expected.

for python_name, jsii_name in python_jsii_mapping(
struct
for python_name, jsii_name in (
python_jsii_mapping(struct) or {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch...I imagine this is a consequence of us running pyright on this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct.

_instances: Mapping[Type[Any], Any] = {}
_instances: MutableMapping[Type[Any], Any] = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find any references to this property in this PR...why make this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _instances value is mutated, and Mapping is immutable... This should always have been typed as MutableMapping but mypy was more forgiving than pyright here...

Copy link
Contributor

@comcalvi comcalvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for clarifying, this looks great.

@mergify
Copy link
Contributor

mergify bot commented Aug 10, 2022

Thank you for contributing! ❤️ I will now look into making sure the PR is up-to-date, then proceed to try and merge it!

@mergify mergify bot added the pr/ready-to-merge This PR is ready to be merged. label Aug 10, 2022
@mergify
Copy link
Contributor

mergify bot commented Aug 10, 2022

Merging (with squash)...

@mergify
Copy link
Contributor

mergify bot commented Aug 11, 2022

Merging (with squash)...

@mergify mergify bot merged commit 5413720 into main Aug 11, 2022
@mergify mergify bot deleted the rmuller/py-class-properties branch August 11, 2022 09:24
@mergify mergify bot removed the pr/ready-to-merge This PR is ready to be merged. label Aug 11, 2022
mergify bot pushed a commit that referenced this pull request Sep 23, 2022
Corrects multiple issues with running `bin` scripts from python.

These were discovered in the context of this `projen` issue: projen/projen#2103

 - Corrects broken argument marshaling to binary scripts introduced in #3694
 - Exposes exit code and stderr from script execution instead of failing silently

Additionally, adds more robust test coverage of passing arguments to `bin` scripts.

---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(Python): @jsii.python.classproperty not working as intended with type checking
2 participants