`TypedMapping` for extended compatibility?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 28/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- python
- Domain
- developer-experience
Research direction
Start with the TypedDict, Mapping, and MappingProxyType examples in the issue. Trace the reported assignment incompatibility and compare the requested TypedMapping behavior with existing typing rules; done means documenting a supported workaround or producing a clearly scoped feature proposal.
Written by the indexing model from the issue text.
Description
Hello,
Consider the following, where lock aims at making provided argument immutable.
@dataclass
class Works:
foo: Mapping
def lock(self) -> None:
self.foo = MappingProxyType(self.foo)
It works great with mypy, no issue. Now imagine I want to enforce a bit more to foo by requesting it be compatible with a Movie. I would try the following, without success.
class Movie(TypedDict):
author: str
year: int
@dataclass
class Fails:
foo: Movie
def lock(self) -> None:
self.foo = MappingProxyType(self.foo) # error: Incompatible types in assignment (expression has type "MappingProxyType[str, object]", variable has type "Movie") [assignment]
Is there an workaround? If not, is if a good idea to discuss the possibility of a TypedMapping, that would simply work as follow?
class Movie(TypedMapping): # New feature ?
author: str
year: int
@dataclass
class Fixed:
foo: Movie
def lock(self) -> None:
self.foo = MappingProxyType(self.foo) # Would work
Thanks for your time.
All the best!
Élie.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/typing
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
topic: typing spec
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
topic: documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
topic: conformance tests topic: typing spec
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Similar issues
-
Add: hunch Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
AbdelStark/awesome-typesafe#104 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
DiamondLightSource/dodal#2211 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
openml/openml-python#1749 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
sipyourdrink-ltd/bernstein#6191 ·