Open
Description
If settings.toml
contains:
A_NUMBER = 123
os.getenv("A_NUMBER")
will return the int
123
.
This is non-standard compared with CPython, where os.getenv()
always returns a string or None
.
This came up because I was thinking about adding TOML bool support (true
and false
) to what we can parse in settings.toml
.
I'm not sure this is worth fixing, but I think it's possible to fix.
Activity
jepler commentedon Mar 29, 2024
It's worth noting that the formula
x = int(getenv("VAR"))
will be portable between circuitpython & standard python.NateChurch commentedon Jun 14, 2025
I ran into a related problem because of my assumptions.
in my settings.toml
I run
and I get this error
If I alter my settings.toml
then it runs fine. I had assumed it would always return a string but it doesn't. I also assumed it was trying to make a float because it has a period in it, but that isn't a thing either. It wants strings to have quotes.
getenv: Make os.getenv() show a better error
dhalbert commentedon Jun 14, 2025
@NateChurch TOML wants strings to have quotes: https://toml.io/en/
dhalbert commentedon Jun 14, 2025
See
#10422 (comment):
from @Neradoc:
and
#10422 (comment)
from @jepler:
dhalbert commentedon Jun 14, 2025
The fundamental problem, as we know, is that TOML provides multiple data types, and
os.getenv()
is only strings.I see some different possibilities:
os.getenv()
on a non-string value. This is getenv: Make os.getenv() show a better error #10422 as of now.os.getenv()
to always return the value as a string. The user code can parse the string as appropriate. EDIT: I started to do this, just by changingcircuitpython/shared-module/os/getenv.c
Lines 379 to 383 in 325d49c
But this isn't quite right, because it picks up trailing spaces from things like
v = 7 # comment
.4. Add something like
supervisor.get_setting(key)
which will return a typed value consistent with TOML typing.3 and 4 could be done together.
tannewt commentedon Jul 11, 2025
I agree that 3 and 4 would be good.