`TypedMapping` for extended compatibility?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 302
- Avg merge
- 23h
- Merged PRs (30d)
- 8
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.
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.
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.
Assessment
- Tech stack
- python
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100