python / python/mypy

Add original attribute fullname to `AttributeContext` for `get_attribute_hook`

Open
#11,500 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

Allow a get_attribute_hook plugin to see the fullname which was used inside of the hook method.

Currently, the interface for a plugin which handles custom __getattr__ looks like so:

def hook_func(ctx: AttributeContext) -> Type:
    ...

class MyPlugin(Plugin):
    def get_attribute_hook(self, fullname: str) -> ...:
        return hook_func

However, this loses fullname in the context of hook_func.

I've been reading through the various attributes of AttributeContext, and it appears that fullname is lost.
It would be great, for this specific case, to have access to ctx.fullname, as the name which was used may be of particular interest to hook_func.

Pitch

I'm exploring a plugin which handles the types of an object which defines a custom __getattr__.
The trouble is that the object is basically a proxy (#5523), and therefore the type of any given attribute depends on its name.

In #5523, I provided a toy example of a "listifying" proxy type.

I should also note my real use-case, which is more interesting and better justified. At work, I defined a proxy-like object which handles paginated web APIs:
https://github.com/globus/globus-sdk-python/blob/cf91ae8b1b613997b5d706a85a4602df3126e5e9/src/globus_sdk/paging/table.py#L61

This is meant to provide X.paginated.foo as a wrapper over X.foo which iterates over multiple X.foo calls.

I was able to define a plugin which handles this by making the proxy-like object a generic whose type-argument is the class which it wraps. The plugin can then check foo in the proxy against foo in the type-argument, and derive the correct type information. However, this requires access to the name foo, which does not appear to be otherwise available. The only way I could solve was by making get_attribute_hook return a new callable for each name it sees, acting as a closure over that name. i.e.

def hook_gen(name: str):
    def hook_func(ctx: AttributeContext) -> Type:
        ...
    return hook_func

class MyPlugin(Plugin):
    def get_attribute_hook(self, fullname: str) -> ...:
        return hook_gen(fullname)

This is inefficient and unintuitive. It would be better to have the fullname available without this layer of indirection.

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 locating AttributeContext and the get_attribute_hook plugin entry point, then inspect existing plugin API tests for attribute hooks. Add access to the fullname used by the hook and cover it with a regression test; done means a hook can read the requested attribute name without creating a closure.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.