You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Noticed while looking into #12919, somewhat related to #7476
In configparser, SectionProxy acts as a view to a particular section from a RawConfigParser/ConfigParser. It implements various get*() methods (get(), getint(), getfloat(), etc.) which behave like the corresponding methods on RawConfigParser/ConfigParser, but with a notable exception: when the option being retrieved does not exist, all Section.get*() methods return None whereas the RawConfigParser.get*() methods raise an exception.
The reason for this discrepancy has to do with the fact that SectionProxy.get uses a default of fallback=None in CPython, whereas RawConfigParser.get uses a default of fallback=_UNSET. Presumably, this is done to make SectionProxy behave more like a Mapping. All other SectionProxy.get* methods inherit this behavior from SectionProxy.get.
Currently, the SectionProxy.get* methods use essentially the same overloads as RawConfigParser.get*, which (incorrectly) suggest that returning None isn't possible when fallback isn't specified:
At first I thought to add | MaybeNone here, since changing the first overload to return | None has a high chance of being disruptive. However, in this case I wonder if returning | None might be preferable, since
the likelihood of None being returned seems quite high (missing options are common), and
the migration path for affected code would be fairly straightforward (users can switch from section.get(x) to section[x], or provide an explicit fallback argument).
Thoughts?
The text was updated successfully, but these errors were encountered:
Noticed while looking into #12919, somewhat related to #7476
In
configparser
,SectionProxy
acts as a view to a particular section from aRawConfigParser
/ConfigParser
. It implements variousget*()
methods (get()
,getint()
,getfloat()
, etc.) which behave like the corresponding methods onRawConfigParser
/ConfigParser
, but with a notable exception: when the option being retrieved does not exist, allSection.get*()
methods returnNone
whereas theRawConfigParser.get*()
methods raise an exception.Here's an example using
.getint()
:The reason for this discrepancy has to do with the fact that
SectionProxy.get
uses a default offallback=None
in CPython, whereasRawConfigParser.get
uses a default offallback=_UNSET
. Presumably, this is done to makeSectionProxy
behave more like aMapping
. All otherSectionProxy.get*
methods inherit this behavior fromSectionProxy.get
.Currently, the
SectionProxy.get*
methods use essentially the same overloads asRawConfigParser.get*
, which (incorrectly) suggest that returningNone
isn't possible whenfallback
isn't specified:typeshed/stdlib/configparser.pyi
Lines 315 to 318 in 355abb1
At first I thought to add
| MaybeNone
here, since changing the first overload to return| None
has a high chance of being disruptive. However, in this case I wonder if returning| None
might be preferable, sinceNone
being returned seems quite high (missing options are common), andsection.get(x)
tosection[x]
, or provide an explicitfallback
argument).Thoughts?
The text was updated successfully, but these errors were encountered: