On Wed, Aug 16, 2017 at 2:53 AM, Jelle Zijlstra <jelle.zijls...@gmail.com> wrote: [..] >> >> The below is an example of how context items can be used:: >> >> my_context = sys.new_context_item(description='mylib.context') >> my_context.set('spam') > > > Minor suggestion: Could we allow something like > `sys.set_new_context_item(description='mylib.context', > initial_value='spam')`? That would make it easier for type checkers to infer > the type of a ContextItem, and it would save a line of code in the common > case. > > With this modification, the type of new_context_item would be > > @overload > def new_context_item(*, description: str, initial_value: T) -> > ContextItem[T]: ... > @overload > def new_context_item(*, description: str) -> ContextItem[Any]: ... > > If we only allow the second variant, type checkers would need some sort of > special casing to figure out that after .set(), .get() will return the same > type.
I think that trying to infer the type of CI values by its default value is not the way to go: ci = sys.ContextItem(default=1) Is CI an int? Likely. Can it be set to None? Maybe, for some use-cases it might be what you want. The correct way IMO is to extend the typing module: ci1: typing.ContextItem[int] = sys.ContextItem(default=1) # ci1: is an int, and can't be anything else. ci2: typing.ContextItem[typing.Optional[int]] = sys.ContextItem(default=42) # ci2 is 42 by default, but can be reset to None. ci3: typing.ContextItem[typing.Union[int, str]] = sys.ContextItem(default='spam') # ci3 can be an int or str, can't be None. This is also forward compatible with proposals to add a `default_factory` or `initializer` parameter to ContextItems. Yury _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/