[plugins/help] Triggering node visits?
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Hello there 👋
First of all, I post here instead of on Gitter because I think my question is a bit long and it would pollute the thread while being hard to respond too. Also, Thanks for all the documentation in source code, it helps a lot already!
I'm trying to write a plugin for my paramclasses library and I would love some help! For a starting point, I'll focus on my protected function/decorator, which essentially provides runtime equivalent for both typing.finaland typing.Final during paramclasses' definition:
class Foo(ParamClass):
x = protected(0) # Unmodifiable in instances and subclasses
@protected
def bar(self) -> None:
"""Works on methods."""
@protected
@property
def baz(self) -> int:
"""Also works on properties."""
return 0
I started understanding a bit more how mypy plugins work, and I currently have the following:
METAPARAMCLASS_FULLNAME = "paramclasses.paramclasses._MetaParamClass" # Paramclasses have this as metaclass
class CustomPlugin(Plugin):
"""Help mypy undestand ``@protected`` and ``protected()``."""
def get_metaclass_hook(self, fullname) -> Callable[[ClassDefContext], None] | None:
"""Update paramclass definition."""
return paramclass_finder_hook
def get_base_class_hook(self, fullname) -> Callable[[ClassDefContext], None] | None:
"""Update paramclass definition."""
return paramclass_finder_hook
def paramclass_finder_hook(ctx: ClassDefContext):
"""Identify paramclasses and trigger class def modification."""
mcs = ctx.cls.info.metaclass_type
if mcs is not None and mcs.type.fullname == METAPARAMCLASS_FULLNAME:
modify_paramclass_def(ctx.cls)
def modify_paramclass_def(cls: ClassDef) -> None:
"""Handle ``@property`` and ``protected()``."""
replace_protected_decorator_with_final(cls)
replace_protected_assignment_with_Final(cls)
def replace_protected_decorator_with_final(cls: ClassDef) -> None:
"""Visit decorator nodes, move @protected, add to original_decorators, mark node as final."""
# Help pls!
As you can see, I think I'm pretty close to a first POC, I just need to know how to visit cls in replace_protected_decorator_with_final and modify targetted nodes. For that, I'll take inspiration from mypy.semanal.SemanticAnalyzer.visit_decorator() I think?
But I have so many questions:
- Is my overall approach sound/in the spirit of mypy plugins API?
- How can I trigger a visit? I thought about writing a custom semantic analyzer but I don't know how to "trigger" it?
- If I write one, should my
SemanticAnalyzerinherit from(NodeVisitor[None], SemanticAnalyzerInterface, SemanticAnalyzerPluginInterface)just like the one from mypy/semanal.py? I don't really understand what's useful and when. - Should I carry out both
replace_protected_decorator_with_finalandreplace_protected_assignment_with_Finalsimultaneously with a unique semantic analyzer?
Thanks a lot in advance!!!
Élie
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit den Plugin-Hook-Implementierungen im Issue und lies mypy/semanal.py, insbesondere SemanticAnalyzer.visit_decorator(). Ermittle, ob die bestehende Plugin-API das Besuchen und Ändern der anvisierten Klassenknoten unterstützt und ob ein benutzerdefinierter semantischer Analysator angemessen ist; abgeschlossen ist die Aufgabe, wenn ein unterstützter Ansatz festgestellt oder dokumentiert wurde, dass die angeforderte Traversierung nicht verfügbar ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100