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

Code completion fails for OIDC connections #43

Open
Andy-Grigg opened this issue Jan 4, 2022 · 2 comments
Open

Code completion fails for OIDC connections #43

Andy-Grigg opened this issue Jan 4, 2022 · 2 comments
Assignees
Milestone

Comments

@Andy-Grigg
Copy link
Contributor

Andy-Grigg commented Jan 4, 2022

When the connection builder adds OIDC auth, it delegates to a separate OIDC builder. When the OIDC builder completes, it returns a reference to the original connection builder which was stored as an instance variable on the OIDC builder instance.

This works fine when executing the Python, however static type checking fails because the type of the connection builder is not fixed. The connection builder is generally sub-classed, and so the connection builder type cannot be hard coded. The current implementation hardcodes the base connection builder class, which is incorrect if the connection builder has been sub-classed. As a result, the type returned by the completion of the OIDC builder is incorrect, which breaks intellisense in both PyCharm and VS Code.

See #40 for more details.

@Andy-Grigg Andy-Grigg added this to the Jan2022 milestone Jan 4, 2022
@Andy-Grigg Andy-Grigg changed the title Code completion fails for OIDC cconnections Code completion fails for OIDC connections Jan 4, 2022
@Andy-Grigg
Copy link
Contributor Author

Andy-Grigg commented Jan 5, 2022

A few of the things which have been investigated and determined not to work:

  • Curiously Recurring Template patterns https://stackoverflow.com/questions/17164375/subclassing-a-java-builder-class (passes mypy analysis, but pylance and pycharm don't autocomplete properly)
  • TypeVars and/or overloads: These generally help in the case where a specific arg type is either the same type that should be returned, or maps to a specific return type. This doesn't apply here, because the oidc builder class is not subclassed, and therefore the self argument is unchanged
  • Delete type hints for the OIDC finalizing methods. In this case, the pylance and pycharm type checkers determine the returned type to be the base class, not the sub-class.

@Andy-Grigg
Copy link
Contributor Author

One workaround that does work is to define a new function in the bomanalytics connection module, something like this:

def cast_connection(cxn: common.ApiClient) -> "BomAnalyticsClient":
    return cast(BomAnalyticsClient, cxn)

This can then be used in the final script:

from ansys.grantami.bomanalytics import Connection, cast_connection

cxn_ = Connection(server_url).with_oidc().authorize().connect()
cxn = cast_connection(cxn_)

This results in a cxn object with the correct type.

The obvious drawback with this is that it requires some additional boilerplate code to be written by the end user. I can't see a way around this though; once the static typechecker hits .authorize(), the link to the bomanalytics code is broken for this statement. It can only be introduced by another statement that explicitly performs a cast.

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

No branches or pull requests

2 participants