Per type instance metadata
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
I've been working on pynamodb-mypy (started a while ago and coming back to it now...). The first task I took up is to implement attribute optionality, i.e.
from pynamodb.models import Model
from pynamodb.attributes import NumberAttribute
class MyModel(Model):
my_number = NumberAttribute()
my_optional_number = NumberAttribute(null=True)
reveal_type(MyModel().my_number) # note: Revealed type is 'int'
reveal_type(MyModel().my_optional_number) # note: Revealed type is 'Optional[int]'
I've been implementing:
get_function_hookto handle NumberAttribute initialization and parsing the 'null' parameterget_attribute_hookto wrap the return type in optionality as needed
This required storing state between the function hook (parsing the arguments) and the attribute hook. One approach I've successfully tried is to append a "sentinel" type arg to the instance's args, i.e. from NumberAttribute it turns into NumberAttribute[None], and generally from SomeAttribute[...] to SomeAttribute[..., None] (I'm using a NoneType as the sentinel). Then in get_attribute_hook I'm looking for the sentinel.
You can see my current implementation here.
I was wondering what would be your thoughts on storing extra metadata on a type instance. Type args appear to be the only sanctioned "extras" as they're effectively part of the type. The new Annotated comes to mind, e.g. have my function hook return something like Annotated[AttrT, 'nullable_attr'], but I got the impression that it doesn't quite work this way -- that Annotateds should be converted to something else during semantic analysis rather than keeping them around for later stages to use.
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 by reading pynamodb_mypy/plugin.py, especially get_function_hook and get_attribute_hook, then review mypy's handling of type arguments and Annotated during semantic analysis. The issue is complete only once maintainers decide and document a supported way to carry per-instance metadata for optionality; no concrete acceptance criteria are given.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers, devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 18/100