Plugin hook to customize __repr__ of an Instance
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
For example, in Django, there's this QuerySet object with a lot of methods, and to properly support those methods we need two (and maybe more in the future) generic parameters.
With get_type_analyze_hook I can convert QuerySet[MyModel] into QuerySet[MyModel, MyModel], so in the code user don't need to think about number of generics. But in error messages, all those generics values are present, though they don't have any semantics, it's just implementation detail.
Or, for Field, I need to have two generic params, one for type of __set__, one for __get__, so they appear in error message as (CharField for example)
django.db.models.fields.CharField[Union[builtins.str, builtins.int, django.db.models.expressions.Combinable], builtins.str]
None of this info makes any sense to users, so I want it to just be django.db.models.fields.CharField.
This is monkeypatching version that works for one of the cases, should be simple enough to add hook for that,
def make_queryset_repr_return_only_one_type():
from mypy.types import TypeStrVisitor
old_visit_instance = TypeStrVisitor.visit_instance
def patched_visit_instance(self, t: Instance) -> str:
if t.type.has_base(helpers.QUERYSET_CLASS_FULLNAME):
return old_visit_instance(self, helpers.reparametrize_instance(t, [t.args[0]]))
else:
return old_visit_instance(self, t)
TypeStrVisitor.visit_instance = patched_visit_instance
I can make a PR, if you approve it as a viable feature.
Contributor guide
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 TypeStrVisitor.visit_instance and the existing get_type_analyze_hook mechanism, using the shown monkeypatch as the behavioral reference. Determine how a supported plugin hook would customize Instance rendering, and consider QuerySet and Field examples as the completion criteria: implementation-only generic arguments should no longer appear in user-facing type errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100