One more type hinting issue

6 views
Skip to first unread message

Matthew Berry

unread,
Jul 5, 2024, 3:09:09 PMJul 5
to xlSlim Support
Hey Russel,

When passing this string '{"srt_StatementGeographicalAxis": "srt_AmericasMember"}' as the dim_mem_pairs argument, the following function works fine when called from Python. It also works fine if called with an int:

def FilingFacts(url:str, concept_id:Optional[str]=None, dim_mem_pairs:Optional[Union[str,int]]=None, fiscal_period:Optional[str]=None, refresh:Optional[bool]=False) -> List[List]|None:
...

When called from xlSlim with an int, it also works fine. But when called with the string, we get the error: 
#ERR The function call failed - Value {"srt_StatementGeographicalAxis": "srt_AmericasMember"} could not be converted to Int32. 

The issue is related to the type hints. It works in xlSlim if we rewrite it as:
def FilingFacts(url:str, concept_id:Optional[str]=None, dim_mem_pairs:str|int|None=None, fiscal_period:Optional[str]=None, refresh:Optional[bool]=False) -> List[List]|None:
...

which is also valid python as of 3.10, but it's ugly mixing syntaxes.

Can you see if this replicates for you?

Thanks,
Matt







xlSlim Dev

unread,
Jul 6, 2024, 7:07:02 AMJul 6
to xlSlim Support
Hi Matt,

The short answer is try dim_mem_pairs:Optional[Any]=None

The Excel C API only supports a limited set of types, so xlSlim evaluates the function and it's type hints to try determine the best types to use when registering Excel functions. What you are seeing in your original function is that xlSlim is determining that dim_mem_pairs is an optional integer:

Screenshot 2024-07-06 114907.png

Excel has no equivalent of string or integer, the closest is Object which can be anything.

Amending the function to use the type hint Optional[Any]
def FilingFacts(url:str, concept_id:Optional[str]=None, dim_mem_pairs:Optional[Any]=None, fiscal_period:Optional[str]=None, refresh:Optional[bool]=False) -> List[List]|None:
results in xlSlim registering the function using the Object type like this:

Screenshot 2024-07-06 114719.png
which should be what you are looking for.

The best solution is to write functions that expect only one type for each argument so xlSlim can match Python to Excel types as closely as possible.

Regards,
Russel

Matthew Berry

unread,
Jul 15, 2024, 2:23:09 PM (12 days ago) Jul 15
to xlSlim Support
Thanks, Russel. That solution works.
Reply all
Reply to author
Forward
0 new messages