Evandro Pomatti
Evandro Pomatti

Reputation: 15124

Declare or "cast" to actual type in Python when returning 'any' from function

Function that I'm consuming returns any as type and I can't get suggestions in the code.

How can I make my core_client variable be of the actual type <CoreClient> <azure.devops.released.core.core_client.CoreClient>?

I tried looking into any specification but couldn't find a guide for it there.

What are the options to make it work?

This is the SDK function I'm calling:

def get_core_client(self):
    """get_core_client.
    Gets the 5.1 version of the CoreClient
    :rtype: :class:`<CoreClient> <azure.devops.released.core.core_client.CoreClient>`
    """
    return self._connection.get_client('azure.devops.released.core.core_client.CoreClient')

I can't get any suggestions, seems like the any return type is the cause:

core_client = connection.clients.get_core_client()
core_client.*** # no suggestions here

SDK is from https://pypi.org/project/azure-devops/

enter image description here

Upvotes: 0

Views: 50

Answers (1)

Joran Beasley
Joran Beasley

Reputation: 113998

core_client:CoreClient = connection.clients.get_core_client()

then you should get hints.... (depending on your IDE)

typically ides also understand isinstance

if you add assert isinstance(core_client,CoreClient) most IDE's will (some will recognize a block of if isinstance(core_client,CoreClient):) and pick this up as well, but generally its not a great idea to use asserts in production code (but you can...)

keep in mind of coarse specifying types does not specify any kind of enforceable contract(core_client:CoreClient = ...) is really just for syntax assistance (and autodoc stuff...)

Upvotes: 3

Related Questions