Type casting for str parameters

27 views
Skip to first unread message

Matthew Berry

unread,
Mar 8, 2024, 9:11:29 AMMar 8
to xlSlim Support
Hey Russel,

I have an issue getting a None default value for a string parameter.

In the function below, regardless of whether I call it with or without values, both a and b are cast as strings.

def test_string(a:str, b:str=None) -> str:
return f"a: {a}\ntype(a):{type(a)}\nb: {b}\ntype(b):{type(b)}"

For example, called from Excel as test_string(""), gives:

a:
type(a):<class 'str'>
b:
type(b):<class 'str'>

I should get a="" and b=NoneType

Is this the expected behaviour?  I think not, since we need to be able to distinguish no input (None) from empty string input.

Any ideas?
Matt  

xlSlim Dev

unread,
Mar 8, 2024, 11:37:12 AMMar 8
to xlSlim Support
Hi Matt,

You are right, the code is failing to detect the empty string correctly. Internally the code receives "" rather than the expected ExcelMissing constant. This is easily fixable, I'll correct this in the next release.

This is one of the few cases where a function with no type hints will give a better answer as parameters of type object are correctly detected as ExcelMissing.

def test_string(a, b=None):

return f"a: {a}\ntype(a):{type(a)}\nb: {b}\ntype(b):{type(b)}"

works fine.

It also works fine with a list of strings:

def test_string(a:str, b:List[str]=None)->str:
...

also works as expected.

Thanks for raising this.

All the best,
Russel

xlSlim Dev

unread,
Mar 14, 2024, 1:04:21 PMMar 14
to xlSlim Support
Hi Matt,

The empty string handling is corrected in 3.2.0

Regards,
Russel

Matthew Berry

unread,
Mar 15, 2024, 2:22:24 PMMar 15
to xlSlim Support
I just tested but now it seems the issue is reversed :) 

Using "" (empty string) as an input in Excel is translated into NoneType within Python :)

xlSlim Dev

unread,
Mar 16, 2024, 7:37:50 AMMar 16
to Matthew Berry, xlSlim Support
That is excepted behaviour, there is no way to distinguish between a missing value and an empty string on my side as Excel passes both as an empty string. I think it is ok as Python code often treats an empty string as False or None.

--
You received this message because you are subscribed to the Google Groups "xlSlim Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xlslim-suppor...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/xlslim-support/cfc4a571-2605-4353-9c3f-6fbd9932910an%40googlegroups.com.

xlSlim Dev

unread,
Mar 16, 2024, 7:39:13 AMMar 16
to Matthew Berry, xlSlim Support
That should be “expected behaviour “, darned autocorrect 😊

Matthew Berry

unread,
Mar 16, 2024, 8:11:24 AMMar 16
to xlSlim Dev, xlSlim Support
Hmmm 🤔

Then how does one pass an empty string, specifically? Rather than None for a missing parameter? 

xlSlim Dev

unread,
Mar 16, 2024, 10:21:54 AMMar 16
to xlSlim Support
I think the behaviour is ok, the main change is that missing string inputs are now detected and defaults used.

For example this function:

def test_string2(a, b="Default string"):

    return f"a: {a}\ntype(a):{type(a)}\nb: {b}\ntype(b):{type(b)}"  

Will use "Default String" if b is not supplied.

I don't really see an issue with not being able to pass in an empty string. If you really needed to you could do:

def test_string3(a, b=""):


    return f"a: {a}\ntype(a):{type(a)}\nb: {b}\ntype(b):{type(b)}"   

So define the function with a default of an empty string.
Reply all
Reply to author
Forward
0 new messages