PlasmaPy / PlasmaPy/PlasmaPy

Update @particle_input to handle typing that utilzes Union[], List[], etc.

Open
#912 10 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

Plasma Lv0 | Novice plasmapy.particles Python Lv3 | Proficient
Dominant language
Python
Stars
711
Forks
375
Avg merge
3h 2m
Merged PRs (30d)
8

Description

@particle_input ques off of arguments annotated with Particle to properly condition that argument into a Particle() object. However, that argument is allowed to be a string (e.g. "He+") or a Particle() object and, thus, should really be annotated with Union[str, Particle]. Without this annotation, IDEs will generally throw warnings if you trying to use "He+" as an argument.

I think @particle_input can accomplish this by doing something like...

def find_particle(func):
     def find_in_depth(args):
         for arg in args:
             if hasattr(arg, "__args__"):
                 result = find_in_depth(arg.__args__)
             else:
                 result = Particle == arg
             
             if result:
                 break
 
         return result
 
     annos = func.__annotations__
     found = {}
 
     for name, anno in annos.items():
         if hasattr(anno, "__args__"):
             results = find_in_depth(anno.__args__)
         else:
             results = Particle == anno

         found[name] = results

     return found

Then the following would result in...

>>> def foo(ion: Union[str, Particle], par: Particle, p2: List[Particle], arg: [u.cm, u.kg]):
...     pass
>>> find_particle(foo)
{'ion': True, 'par': True, 'p2': True, 'arg': False}

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.