python / python/mypy

How to make a class Unpackable from plugin hook.

Open
#10,208 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question topic-plugins
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Hi guys,

I am trying to add mypy support for dataclassy, which is a replacement for standard dataclasses.

You can see the code here:
https://github.com/biqqles/dataclassy/blob/15cef524b55df2c65b39d1619c7b8f1a3d28a813/dataclassy/mypy.py

One of the problems I've run in to is in supporting the feature of dataclassy that allows its dataclasses to be unpacked like tuples.

Firstly, I couldn't figure out how to represent tuple style unpacking in the mypy type system. If I create a class with def __iter__(self) -> Iterable[int], then it works, but I can't seem to do anything like Iterable[int, int, str, bool]. When I reveal_type for a NamedTuple.__iter__ I just see a generic iterator typing.Iterator[_T_co]. I see that reveal_type((1, 'foo').__iter__()) is typing.Iterator[builtins.object*]

So I tried to figure out if there was special logic in semanal_namedtuple.py but I couldn't see anything there other than adding Iterable as a base in the _make method(?) which doesn't seem like that I need. And, besides, I figure wherever the logic is it must also apply to regular tuples too.

What am I missing?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading dataclassy/mypy.py and mypy's semanal_namedtuple.py, especially the referenced _make method, then reproduce the reveal_type examples for tuple and NamedTuple iteration. Done means identifying how, or whether, tuple-style unpacking can be represented through the mypy plugin hook and giving a concrete answer for this use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.