Skip to content

SONARPY-957 Protobuf Typeshed should serialize information about variables#1041

Merged
andrea-guarino-sonarsource merged 3 commits into
MMF-2578from
SONARPY-957
Jan 5, 2022
Merged

SONARPY-957 Protobuf Typeshed should serialize information about variables#1041
andrea-guarino-sonarsource merged 3 commits into
MMF-2578from
SONARPY-957

Conversation

@andrea-guarino-sonarsource
Copy link
Copy Markdown
Contributor

No description provided.

@andrea-guarino-sonarsource andrea-guarino-sonarsource marked this pull request as ready for review January 3, 2022 15:35
@andrea-guarino-sonarsource andrea-guarino-sonarsource changed the title SONARPY-957 rotobuf Typeshed should serialize information about variables SONARPY-957 Protobuf Typeshed should serialize information about variables Jan 3, 2022
Copy link
Copy Markdown
Contributor

@guillaume-dequenne guillaume-dequenne left a comment

Choose a reason for hiding this comment

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

Looks good, but I think this highlights an incomplete handling for type classes from MyPy (which is my bad really...) and we should probably do something about it to avoid risking some unwanted behavior for those classes.

def __init__(self, _type: mpt.Type):
self.args = []
self.fully_qualified_name = None
self.kind = None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you had to add this, I assume that means that the final else on line 132 can actually be triggered. As this shows, it wasn't a robust piece of code, but back when I wrote this I actually assumed this condition could not be triggered because all "common" type classes encountered in TypeShed were covered (and I just wanted to avoid a failure if it actually wasn't the case due to some unsupported edge case).

Do you know on which case it happens? Maybe the comment on line 133 can be updated to mention when it is triggered, or maybe there are other type classes we can actually handle here.

EDIT: At least one case where it seems to happen is when setting an alias for an overloaded symbol. For example in gettext.pyi I see multiple overloaded definitions for the function translation, then a variable definition:

Catalog = translation

This ultimately means we serialize variable tranlsation to have a type annotation such as:

  type_annotation {
    pretty_printed_name: "#Unknown"
  }

Maybe we shouldn't do that and just not serialize any type annotation? Or maybe from the actual type class we see from mypy, we can infer this is an overloaded function and actually serialize that (I see the type class Overload actually exist in mypy).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point! I added self.kind = None because indeed I had trouble with variables pointing to aliases of overloaded symbols and I then forgot to handle the "#Unknown" types.
I think we can just avoid to serialize any type annotation in that case, and maybe improve the logic and try to get the actual type class in another ticket / PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you create a ticket for that? I don't think it major but I guess it would provide a small precision boost in some cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, I created SONARPY-960

this.name = varSymbol.getName();
this.fullyQualifiedName = TypeShed.normalizedFqn(varSymbol.getFullyQualifiedName());
String fqn = varSymbol.getTypeAnnotation().getFullyQualifiedName();
if (!fqn.isEmpty()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Related to the previous comment, what happens here on a serialized type name that is #Unknown?

Copy link
Copy Markdown
Contributor Author

@andrea-guarino-sonarsource andrea-guarino-sonarsource Jan 5, 2022

Choose a reason for hiding this comment

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

I think we can prevent this to happen in the serializer. We could also add here a check that would throw an exception if type name is #Unknown

Copy link
Copy Markdown
Contributor Author

@andrea-guarino-sonarsource andrea-guarino-sonarsource Jan 5, 2022

Choose a reason for hiding this comment

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

Thinking again about this: #Unknown is the pretty_printed_name which we currently don't really use in the deserializer. The fqn instead should never contain #Unknown.

Copy link
Copy Markdown
Contributor

@guillaume-dequenne guillaume-dequenne left a comment

Choose a reason for hiding this comment

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

LGTM

@sonarsource-next
Copy link
Copy Markdown

@andrea-guarino-sonarsource andrea-guarino-sonarsource merged commit cef1df5 into MMF-2578 Jan 5, 2022
@andrea-guarino-sonarsource andrea-guarino-sonarsource deleted the SONARPY-957 branch January 5, 2022 16:34
hashicorp-vault-sonar-prod Bot pushed a commit that referenced this pull request Apr 2, 2026
GitOrigin-RevId: 2e7e792b13b4a267ce09121e77919afb10120b94
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants