-1

I have a function that takes a single string arg.

I think in my first post of this issue I was not clear enought.

The purpose of this question is to figure how how to get typing (autocomplete) support for such methods without using cast.

doc = CreateScritpService("Calc")

To get the type I use cast.

doc = cast(SFDocuments.SF_Calc, CreateScritpService("Calc"))

bas = cast(SFScriptForge.SF_Basic, CreateScriptService("Basic"))

I am the Author of ScriptForge Typings

An I am looking for a way to automatically have CreateScritpService return the correct type based upon the string input arg. Is this possible?

5
  • if statement? dictionary? there are lots of ways to conditionally return things based on string arguments :) Commented Aug 22, 2022 at 22:49
  • Maybe I am missing something. I am looking to get typing support in the IDE from PYI files. Commented Aug 22, 2022 at 22:50
  • 1
    Has Python typing suddenly become the fashionable thing to do? It seems like the number of questions has surged recently. Commented Aug 22, 2022 at 22:52
  • @Barmar I can say for myself that I have written typing's for thousands of classes which have benefited many projects. In my experience when dealing with a large API such as LibreOffice API is much, much more difficult without typings. Commented Aug 22, 2022 at 22:59
  • I just think that today I've seen more typing questions than any other day. Commented Aug 22, 2022 at 23:00

1 Answer 1

1

You can achieve this using overload along with Literal, something like this:

from typing import Literal, overload @overload def CreateScriptService(x: Literal["Calc"]) -> SFDocuments.SF_Calc: ... @overload def CreateScriptService(x: Literal["Basic"]) -> SFScriptForge.SF_Basic: ... 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I took this approach. Seems to work. I had Hundreds of lines to do but its working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.