Hello, I had an idea for a new type annotation called "View" type hint to
indicate attributes that are readable both internally and externally but
should only be writable internally.
Example usage:
from typing import View
class MyClass:
def __init__(self) -> None:
self.my_attr: View[int] = 0
This is equivalent to below from a static type checker's point of view.
class MyClass:
def __init__(self) -> None:
self._my_attr = 0
@property
def my_attr(self) -> int:
return self._my_attr
Updating the Typing Meetup date to Wednesday as there were some conflicts.
Let me know if there are any major time conflicts.
Date:
*Wednesday, November 03, 2021.*Time: 10 am San Francisco time (UTC-7) / 5
pm London time (UTC)
Duration: 1 hour
Potential discussion topics:
+ Typing **kwargs using a TypedDict (Franek Magiera?, Pradeep) [1]
+ Remaining questions about Callable syntax (Steven)
+ Non-required Protocol fields (Sebastian Rittau?) [2]
+ Type support for sealed classes (Adrian Freund?) [3]
+ Safe, typed JSON validation
+ Type system documentation (Shannon?)
If you're working on these or any other topics and would like to start a
discussion at the meetup, reach out to me.
If you have interesting topics you'd like to see discussed, share them too.
Once we have topics, we can always start a discussion even if there's no
proposer yet. We will probably have time for 3-4 topics in this session.
On Fri, Oct 15, 2021 at 12:55 PM S Pradeep Kumar <gohanpra(a)gmail.com> wrote:
>>>
>>>> Hello all,
>>>>
>>>> We plan to hold a virtual Typing Meetup to discuss key typing-related
>>>> issues in real-time. The idea is that we can make progress on important or
>>>> controversial discussions, get early feedback on in-progress PEPs, and
>>>> gauge interest in new proposals.
>>>>
>>>> A virtual meetup will be more accessible to people in other timezones
>>>> than Bay Area in-person meetups. It should also help consolidate
>>>> discussions that are often spread out across mailing lists or Github repos.
>>>> The tentative plan is to have a monthly meetup, but we can change that as
>>>> needed.
>>>>
>>>> Date: Monday, November 01, 2021.
>>>> Time: 10 am San Francisco time (UTC-7) / 5 pm London time (UTC)
>>>> Duration: 1 hour
>>>>
>>>> Time and date are flexible. I'll share the zoom link and agenda the
>>>> week before. I'll also record the meetup and share afterwards.
>>>>
>>>> If you're interested in presenting a new proposal or your work on some
>>>> existing proposal, please respond here. We can shoot for 15 minutes per
>>>> presentation. I know there are quite a few interesting threads on
>>>> typing-sig that haven't seen much activity in a while :)
>>>> --
>>>> S Pradeep Kumar
>>>> _______________________________________________
>>>> Typing-sig mailing list -- typing-sig(a)python.org
>>>> To unsubscribe send an email to typing-sig-leave(a)python.org
>>>> https://mail.python.org/mailman3/lists/typing-sig.python.org/
>>>> Member address: guido(a)python.org
>>>
>>>
Links:
[1] Typing **kwargs using TypedDict:
https://mail.python.org/archives/list/typing-sig@python.org/thread/IUCXT2IQ…
[2] Non-required Protocol fields:
https://mail.python.org/archives/list/typing-sig@python.org/thread/P6FCGRJU…
[3] Sealed classes:
https://mail.python.org/archives/list/typing-sig@python.org/thread/QSCT2N4R…
--
S Pradeep Kumar
I have disabled the typing issues mailer for now. If there is still
interest, I could change it to send the mail to the new Discourse topic
(as soon as it is created), otherwise I would just leave it disabled.
- Sebastian
The following is an overview of all issues and pull requests in the
typing repository on GitHub with the label 'topic: feature'
that were opened or updated last week, excluding closed issues.
---------------------------------------------------
The following issues and pull requests were opened last week:
#1467 Dynamic and Discriminated Unions
opened by @irgolic
https://github.com/python/typing/issues/1467
---------------------------------------------------
The following issues and pull requests were updated last week:
#1216 TypeVarTuple Transformations Before Unpack
opened by @llchan
https://github.com/python/typing/issues/1216
#1304 range typing
opened by @Godwhitelight
https://github.com/python/typing/issues/1304
---------------------------------------------------
All issues and pull requests with the label 'topic: feature'
can be viewed under the following URL:
https://github.com/python/typing/issues?q=label%3A%22topic%3A+feature%22
The following is an overview of all issues and pull requests in the
typing repository on GitHub with the label 'topic: feature'
that were opened or updated last week, excluding closed issues.
---------------------------------------------------
The following issues and pull requests were updated last week:
#1383 Create PEP for Map with bind: match Tuple of parameterized types with parameters from TypeVarTuple
opened by @jmsmdy
https://github.com/python/typing/issues/1383
#1399 Feature request: Allow unpacking of type variable bound to typed dict
opened by @NeilGirdhar
https://github.com/python/typing/issues/1399
#1454 Allow use of `Required` and `NotRequired` to make an existing typed dict total or optional
opened by @CaselIT
https://github.com/python/typing/issues/1454
---------------------------------------------------
All issues and pull requests with the label 'topic: feature'
can be viewed under the following URL:
https://github.com/python/typing/issues?q=label%3A%22topic%3A+feature%22
The following is an overview of all issues and pull requests in the
typing repository on GitHub with the label 'topic: feature'
that were opened or updated last week, excluding closed issues.
---------------------------------------------------
The following issues and pull requests were opened last week:
#1454 Allow use of `Required` and `NotRequired` to make an existing typed dict total or optional
opened by @CaselIT
https://github.com/python/typing/issues/1454
---------------------------------------------------
The following issues and pull requests were updated last week:
#801 Introduce a Not type
opened by @antonagestam
https://github.com/python/typing/issues/801
#1383 Create PEP for Map with bind: match Tuple of parameterized types with parameters from TypeVarTuple
opened by @jmsmdy
https://github.com/python/typing/issues/1383
---------------------------------------------------
All issues and pull requests with the label 'topic: feature'
can be viewed under the following URL:
https://github.com/python/typing/issues?q=label%3A%22topic%3A+feature%22
This post doesn't have any particular aim, just a design report. I'm
curious if anyone else had done this and has experience to share, or
maybe there's a nicer solution for this in Python.
### Motivation
In pytest we have plugin system where external plugins may implement
various hooks. For the sake of example let's say a plugin wants to
do something on each test ("item") setup and teardown:
def pytest_runtest_setup(item: pytest.Item) -> None: ...
def pytest_runtest_teardown(item: pytest.Item) -> None: ...
One thing plugins often want to do is to store/associate some data with
the item on setup, then use it on teardown.
The traditional way plugins did this was to use an attribute on the
`item`:
def pytest_runtest_setup(item: pytest.Item) -> None:
item.my_plugin_attr = MyPluginAttrType(...)
def pytest_runtest_teardown(item: pytest.Item) -> None:
use_my_attr(item.my_plugin_attr)
# Maybe
del item.my_plugin_attr
The problem with this is that type checkers are not aware of
`my_plugin_attr` and they complain. To make things clean for type
checkers, and type safe (no casts/asserts) an alternative solution is
needed.
### Rejected solution 1
We can add a dict to each item on the pytest side:
class Item:
def __init__(self, ...) -> None:
...
self.store: Final[Dict[str, ???]] = {}
which plugins will use as
def pytest_runtest_setup(item: pytest.Item) -> None:
item.store["my_plugin_attr"] = MyPluginAttrType(...)
This fixes the "undefined attribute" issue, but as the `???` indicates,
it's still not good. We can either use `object` and force casts/asserts,
or `Any` and lose type safety.
### Rejected solution 2
We can imagine a typing feature which allows a 3rd-party to "overlay" a
type it did not define with extra fields/methods. I think some languages
support this (TypeScript?). But it does not seem like a desirable feature
to me.
### Solution we ended up with
We define a `Stash` type (because plugins "stash" their data there), which
is used as follows:
# In pytest
class Item:
def __init__(self, ...) -> None:
...
self.stash: Final[Stash] = Stash()
# In plugin
my_plugin_attr: Final = pytest.StashKey[MyPluginAttrType]()
def pytest_runtest_setup(item: pytest.Item) -> None:
item.stash[my_plugin_attr] = MyPluginAttrType(...)
def pytest_runtest_teardown(item: pytest.Item) -> None:
use_my_attr(item.stash[my_plugin_attr])
# Maybe
del item.stash[my_plugin_attr]
Each plugin defines `StashKey`s for its attributes, and the stash key
carries the attribute type which makes things type safe.
The `Stash` is defined like this (see [0] for full implementation in pytest):
T = TypeVar("T")
D = TypeVar("D")
class StashKey(Generic[T]):
__slots__ = ()
class Stash:
__slots__ = ("_storage",)
def __init__(self) -> None:
self._storage: Dict[StashKey[Any], object] = {}
def __setitem__(self, key: StashKey[T], value: T) -> None:
self._storage[key] = value
def __getitem__(self, key: StashKey[T]) -> T:
return cast(T, self._storage[key])
def __delitem__(self, key: StashKey[T]) -> None:
del self._storage[key]
# .. other mutable mapping methods -- same idea.
[0] https://github.com/pytest-dev/pytest/blob/7.4.0/src/_pytest/stash.py
### Advantages
No undefined attributes. Type safe. The `StashKey`s are namespaced
objects, which makes conflicts between plugins impossible, unlike with
string keys.
### Disadvantages
Not standard, users are not familiar with idea and are confused, try to
use it as a dict.
Less ergonomic, need to define the `StashKey`s at the module level.
The following is an overview of all issues and pull requests in the
typing repository on GitHub with the label 'topic: feature'
that were opened or updated last week, excluding closed issues.
---------------------------------------------------
The following issues and pull requests were updated last week:
#256 Possible to distinguish between Sequence[str]/Iterable[str] and str?
opened by @jtatum
https://github.com/python/typing/issues/256
---------------------------------------------------
All issues and pull requests with the label 'topic: feature'
can be viewed under the following URL:
https://github.com/python/typing/issues?q=label%3A%22topic%3A+feature%22