-
Notifications
You must be signed in to change notification settings - Fork 338
/
decorators.py
996 lines (807 loc) · 33.9 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
"""Decorators for `plasmapy.particles`."""
__all__ = ["particle_input"]
import functools
import inspect
import warnings
from collections.abc import Callable, Iterable, MutableMapping
from inspect import BoundArguments
from numbers import Integral, Real
from typing import Any, TypeAlias, TypedDict, get_type_hints
import numpy as np
import wrapt
from plasmapy.particles._factory import _physical_particle_factory
from plasmapy.particles.exceptions import (
ChargeError,
InvalidElementError,
InvalidIonError,
InvalidIsotopeError,
InvalidParticleError,
ParticleError,
)
from plasmapy.particles.particle_class import CustomParticle, Particle, ParticleLike
from plasmapy.particles.particle_collections import ParticleList, ParticleListLike
from plasmapy.utils.exceptions import PlasmaPyDeprecationWarning
class _CallableDataDict(TypedDict, total=False):
allow_custom_particles: bool
allow_particle_lists: bool
annotations: dict[str, Any]
any_of: str | Iterable[str] | None
callable_: Callable[..., Any]
exclude: str | Iterable[str] | None
parameters_to_process: list[str]
require: str | Iterable[str] | None
signature: inspect.Signature
_basic_particle_input_annotations: tuple[type | TypeAlias, ...] = (
Particle, # deprecated
ParticleLike,
ParticleListLike,
ParticleLike | ParticleListLike,
(Particle, Particle), # deprecated
)
_optional_particle_input_annotations = tuple(
annotation | None
# remove [:-1] index in following line when dropping (Particle, Particle)
# as a valid annotation
for annotation in _basic_particle_input_annotations[:-1]
if annotation != (Particle, Particle) # temporary hack
)
_particle_input_annotations = (
_basic_particle_input_annotations + _optional_particle_input_annotations
)
def _make_into_set_or_none(obj: Any) -> Iterable[str] | None:
"""
Return `None` if ``obj`` is `None`, and otherwise convert ``obj``
into a `set`.
If ``obj`` is a string, then a `set` containing only ``obj`` will
be returned (i.e., ``obj`` will not be treated as iterable).
"""
if obj is None:
return obj
return {obj} if isinstance(obj, str) else set(obj)
def _bind_arguments(
wrapped_signature: inspect.Signature,
callable_: Callable[..., Any],
args: Iterable[Any],
kwargs: MutableMapping[str, Any],
instance: Any = None,
) -> inspect.BoundArguments:
"""
Bind the arguments provided by ``args`` and ``kwargs`` to
the corresponding parameters in the signature of the callable_
or method being decorated.
Parameters
----------
wrapped_signature : `inspect.Signature`
The signature of the function or method to which to bind
``args`` and ``kwargs``.
callable_ : callable
The function or method to which to bind ``args`` and ``kwargs``.
This argument is only needed for a deprecation warning message.
args : tuple, optional
Positional arguments.
kwargs : `dict` of `str` to `object`, optional
Keyword arguments.
instance
If ``callable_`` is a class instance method, then ``instance``
should be the instance to which ``callable_`` belongs.
Returns
-------
dict
A dictionary with the parameters of ``callable_`` as keys and
the corresponding arguments as values, but removing ``self`` and
``cls``.
"""
# We should keep the warning about "z_mean" for perhaps ∼2
# releases following the last pull request that removes a "z_mean"
# parameter from a callable decorated with @particle_input. After
# that, we should change this warning to an exception for ∼2 more
# releases before deleting it.
if "z_mean" in kwargs and "Z" not in kwargs and "Z" in wrapped_signature.parameters:
function_name = getattr(callable_, "__name__", None)
name_clause = f"to '{function_name}' " if function_name else ""
warnings.warn(
f"The 'z_mean' parameter {name_clause}has been deprecated "
"and will be removed in a subsequent release. Define the "
"(mean) charge number with 'Z' instead.",
category=PlasmaPyDeprecationWarning,
)
kwargs["Z"] = kwargs.pop("z_mean")
# When decorating a callable_ or staticmethod, instance will
# be None. When decorating a class instance method, instance
# will be the class instance, and will need to be bound to
# the "self" parameter but later removed. For a class method,
# it will be bound to the "cls" parameter instead.
if instance is None:
bound_arguments = wrapped_signature.bind(*args, **kwargs)
else:
bound_arguments = wrapped_signature.bind(instance, *args, **kwargs)
bound_arguments.apply_defaults()
bound_arguments.arguments.pop("self", None)
bound_arguments.arguments.pop("cls", None)
return bound_arguments
class _ParticleInput:
"""
Processes arguments for |particle_input|.
Parameters
----------
callable_ : callable
The callable_ or method to be decorated.
require : `str`, `set`, `list`, or `tuple`, |keyword-only|, optional
Categories that a particle must be in. If a particle is not in
all of these categories, then a |ParticleError| will be raised.
any_of : `str`, `set`, `list`, or `tuple`, |keyword-only|, optional
Categories that a particle may be in. If a particle is not in
any of these categories, then a |ParticleError| will be raised.
exclude : `str`, `set`, `list`, or `tuple`, |keyword-only|, optional
Categories that a particle cannot be in. If a particle is in
any of these categories, then a |ParticleError| will be raised.
allow_custom_particles : bool, |keyword-only|, default: `True`
If `True`, allow |CustomParticle| instances to be passed through.
allow_particle_lists : bool, |keyword-only|, default: `True`
If `True`, allow |ParticleList| instances to be passed through.
"""
def __init__(
self,
callable_: Callable[..., Any],
*,
require: str | Iterable[str] | None = None,
any_of: str | Iterable[str] | None = None,
exclude: str | Iterable[str] | None = None,
allow_custom_particles: bool = True,
allow_particle_lists: bool = True,
) -> None:
self._data: _CallableDataDict = {}
self.callable_: Callable[..., Any] = callable_
self.require = require
self.any_of = any_of
self.exclude = exclude
self.allow_custom_particles = allow_custom_particles
self.allow_particle_lists = allow_particle_lists
@property
def callable_(self) -> Callable[..., Any]:
"""
The callable that is being decorated.
Returns
-------
callable
"""
return self._data["callable_"]
@callable_.setter
def callable_(self, callable_: Callable[..., Any]) -> None:
self._data["callable_"] = callable_
self._data["annotations"] = get_type_hints(callable_)
self._data["parameters_to_process"] = self.find_parameters_to_process()
self._data["signature"] = inspect.signature(callable_)
@property
def signature(self) -> inspect.Signature:
"""The signature of the wrapped callable."""
return self._data["signature"]
def find_parameters_to_process(self) -> list[str]:
"""
Identify the parameters that have annotations to indicate that
they should be processed.
Returns
-------
`list` of `str`
"""
return [
parameter
for parameter, annotation in self.annotations.items()
if annotation in _particle_input_annotations and parameter != "return"
]
@property
def annotations(self) -> dict[str, Any]:
"""
The annotations of the decorated callable_.
Returns
-------
`dict` of `str` to `object`
"""
return self._data.get("annotations") # type: ignore[return-value]
@property
def require(self) -> Iterable[str] | None:
"""
Categories that the particle must belong to.
Returns
-------
`set` of `str`, or `None`
"""
return self._data["require"]
@require.setter
def require(self, require_: str | Iterable[str] | None) -> None:
self._data["require"] = _make_into_set_or_none(require_)
@property
def any_of(self) -> Iterable[str] | None:
"""
Categories of which the particle must belong to at least one.
Returns
-------
`set` of `str`, or `None`
"""
return self._data["any_of"]
@any_of.setter
def any_of(self, any_of_: str | Iterable[str] | None) -> None:
self._data["any_of"] = _make_into_set_or_none(any_of_)
@property
def exclude(self) -> Iterable[str] | None:
"""
Categories that the particle cannot belong to.
Returns
-------
`set` of `str`, or `None`
"""
return self._data["exclude"]
@exclude.setter
def exclude(self, exclude_: str | Iterable[str] | None) -> None:
self._data["exclude"] = _make_into_set_or_none(exclude_)
@property
def allow_custom_particles(self) -> bool:
"""
If `True`, then the decorated argument may be or include
|CustomParticle| instances.
Returns
-------
bool
"""
return self._data["allow_custom_particles"]
@allow_custom_particles.setter
def allow_custom_particles(self, allow_custom_particles_: bool) -> None:
self._data["allow_custom_particles"] = allow_custom_particles_
@property
def allow_particle_lists(self) -> bool:
"""
If `True`, then the decorated argument may be a |ParticleList|.
Returns
-------
bool
"""
return self._data["allow_particle_lists"]
@allow_particle_lists.setter
def allow_particle_lists(self, allow_particle_lists_: bool) -> None:
self._data["allow_particle_lists"] = allow_particle_lists_
@property
def parameters_to_process(self) -> list[str]:
"""
The parameters of
`~plasmapy.particles.decorators._ParticleInput.callable_` that have
annotations to be processed by |particle_input|.
Returns
-------
`list` of `str`
"""
return self._data["parameters_to_process"]
def verify_charge_categorization(
self, particle: Particle | CustomParticle | ParticleList
) -> None:
"""
Raise an exception if the particle does not meet charge
categorization criteria.
Raises
------
|ChargeError|
If the particle is required to have charge information and
does not, or if the particle is required to be charged and
is either uncharged or lacks charge information.
"""
must_be_charged = self.require is not None and "charged" in self.require
must_have_charge_info = self.any_of == {"charged", "uncharged"}
if isinstance(particle, ParticleList):
uncharged = particle.is_category("uncharged", particlewise=True)
lacks_charge_info = particle.is_category(
exclude={"charged", "uncharged"}, particlewise=True
)
else:
uncharged = particle.is_category("uncharged")
lacks_charge_info = particle.is_category(exclude={"charged", "uncharged"})
if isinstance(uncharged, Iterable):
uncharged = any(uncharged)
lacks_charge_info = any(lacks_charge_info)
if must_be_charged and (uncharged or must_have_charge_info):
raise ChargeError(f"{self.callable_} can only accept charged particles.")
if must_have_charge_info and lacks_charge_info:
raise ChargeError(
f"{self.callable_} can only accept particles which have "
f"explicit charge information."
)
@staticmethod
def category_errmsg(
particle: Particle | CustomParticle | ParticleList,
require: str | Iterable[str] | None,
exclude: str | Iterable[str] | None,
any_of: str | Iterable[str] | None,
callable_name: str,
) -> str:
"""
Return an error message for when a particle does not meet
categorization criteria.
Returns
-------
str
"""
category_errmsg = (
f"The particle {particle} does not meet the required "
f"classification criteria to be a valid input to {callable_name}. "
)
errmsg_table = [
(require, "must belong to all"),
(any_of, "must belong to any"),
(exclude, "cannot belong to any"),
]
for condition, phrase in errmsg_table:
if condition:
category_errmsg += (
f"The particle {phrase} of the following categories: {condition}. "
)
return category_errmsg
def verify_particle_categorization(
self, particle: Particle | CustomParticle | ParticleList
) -> None:
"""
Verify that the particle meets the categorization criteria.
Parameters
----------
particle : Particle | CustomParticle
Raises
------
|ParticleError|
If the particle does not meet the categorization criteria.
Notes
-----
This method does not yet work with |ParticleList| objects.
See Also
--------
~plasmapy.particles.particle_class.Particle.is_category
"""
particle_in_category = particle.is_category(
require=self.require,
any_of=self.any_of,
exclude=self.exclude,
)
if not particle_in_category:
errmsg = self.category_errmsg(
particle,
self.require,
self.exclude,
self.any_of,
self.callable_.__name__,
)
raise ParticleError(errmsg)
def verify_particle_name_criteria(
self, parameter: str, particle: Particle | CustomParticle | ParticleList
) -> None:
"""
Check that parameters with special names meet the expected
categorization criteria.
"""
if (
parameter == "ion"
and isinstance(particle, CustomParticle)
and not np.isnan(particle.charge)
and particle.mass.value > 0
):
return None
name_categorization_exception: list[
tuple[str, dict[str, str | Iterable[str] | None], type]
] = [
("element", {"require": "element"}, InvalidElementError),
("isotope", {"require": "isotope"}, InvalidIsotopeError),
(
"ion",
{"require": "element", "any_of": {"charged", "uncharged"}},
InvalidIonError,
),
]
for name, categorization, exception in name_categorization_exception:
if parameter != name or particle is None:
continue
if isinstance(particle, ParticleList):
meets_name_criteria = particle.is_category(
**categorization, particlewise=True
)
else:
meets_name_criteria = particle.is_category(**categorization)
if isinstance(particle, Iterable) and not isinstance(particle, str):
meets_name_criteria = all(meets_name_criteria) # type: ignore[arg-type]
if not meets_name_criteria:
raise exception(
f"The argument {parameter} = {particle!r} to "
f"{self.callable_.__name__} does not correspond to a "
f"valid {parameter}."
)
def verify_allowed_types(
self, particle: Particle | CustomParticle | ParticleList
) -> None:
"""
Verify that the particle object contains only the allowed types
of particles.
"""
if not self.allow_custom_particles and isinstance(particle, CustomParticle):
raise InvalidParticleError(
f"{self.callable_.__name__} does not accept CustomParticle "
f"or CustomParticle-like inputs."
)
if not self.allow_particle_lists and isinstance(particle, ParticleList):
raise InvalidParticleError(
f"{self.callable_.__name__} does not accept ParticleList "
"or particle-list-like inputs."
)
if (
not self.allow_custom_particles
and isinstance(particle, ParticleList)
and any(particle.is_category("custom", particlewise=True)) # type: ignore[arg-type]
):
raise InvalidParticleError(
f"{self.callable_.__name__} does not accept CustomParticle "
f"or CustomParticle-like inputs."
)
def process_argument(
self,
parameter: str,
argument: Any,
Z: float | None,
mass_numb: int | None,
) -> Any:
"""
Process an argument that has an appropriate annotation.
If the annotation is not one covered by |particle_input|, then
this method will return ``argument`` without alteration.
Otherwise, if the annotation is ``Optional[...]`` and
``argument`` is `None`, then this method will return `None`.
Otherwise, this method will use ``argument`` (and possibly ``Z``
and/or ``mass_numb``) to create a |Particle|, |CustomParticle|,
or |ParticleList|. If the resulting particle object does not
meet the specified criteria (if provided), then this method will
raise an exception. Otherwise, the resulting particle object
will be returned. See the docstring for |particle_input| for
more details.
Parameters
----------
parameter : str
The name of the |parameter| that was decorated.
argument : object
The value of the |argument| associated with ``parameter``.
Z : integer, optional
The |charge number| of an ion or neutral particle.
mass_numb : integer, optional
The mass number of an isotope.
Returns
-------
object
This method will return a |Particle|, |CustomParticle|,
|ParticleList|, or `None` if the parameter has an annotation
as described in the docstring for |particle_input|. For all
other annotations, this method will return ``argument``
without alteration.
"""
annotation = self.annotations.get(parameter)
if annotation not in _particle_input_annotations:
return argument
if annotation in _optional_particle_input_annotations and argument is None:
return argument
# This does not yet include cases like Optional[ParticleList],
# Union[ParticleList, ParticleLike], etc. and thus needs updating.
if annotation == (Particle, Particle): # deprecated
if not hasattr(argument, "__len__") or len(argument) != 2:
raise ValueError(f"The length of {argument} must be 2.")
return Particle(argument[0]), Particle(argument[1])
if annotation in _basic_particle_input_annotations and argument is None:
raise TypeError(f"{parameter} may not be None.")
particle = _physical_particle_factory(argument, Z=Z, mass_numb=mass_numb)
self.verify_charge_categorization(particle)
self.verify_particle_categorization(particle)
self.verify_particle_name_criteria(parameter, particle)
self.verify_allowed_types(particle)
return particle
parameters_to_skip = ("Z", "mass_numb")
def perform_pre_validations(self, Z: float | None, mass_numb: int | None) -> None:
"""
Perform a variety of pre-checks on the arguments.
Check that there are annotated parameters. Check that ``Z`` is
a real number if not `None`. Check that ``mass_numb`` is an
integer if not `None`. Verify that ``Z`` and ``mass_numb`` are
not included if there are multiple annotated parameters.
"""
if not self.parameters_to_process:
raise ParticleError(
"No parameters have an annotation that will invoke particle_input."
)
Z_or_mass_numb = Z is not None or mass_numb is not None
multiple_annotated_parameters = len(self.parameters_to_process) > 1
if Z is not None and not isinstance(Z, Real):
raise TypeError("Z must be a real number.")
if mass_numb is not None and not isinstance(mass_numb, Integral):
raise TypeError("mass_numb must be an integer.")
if Z_or_mass_numb and multiple_annotated_parameters:
raise ParticleError(
"The arguments Z and mass_numb are not allowed when more "
"than one argument or keyword is annotated with ParticleLike "
"in callables decorated with @particle_input."
)
def process_arguments(
self,
args: Iterable[Any],
kwargs: MutableMapping[str, Any],
instance: Any = None,
) -> BoundArguments:
"""
Process the arguments passed to the callable_ callable.
Parameters
----------
args : tuple
Positional arguments passed to the callable_ callable.
kwargs : `dict` of `str` to `object`
Keyword arguments provided to the callable_ callable.
instance : `object`, optional
If the callable_ callable is a class instance method, then
``instance`` should be the class instance to which ``func``
belongs.
Notes
-----
This method does not work when there are positional arguments
before variadic positional arguments. See :issue:`2150`.
"""
bound_arguments = _bind_arguments(
self.signature, self.callable_, args, kwargs, instance
)
Z = bound_arguments.arguments.pop("Z", None)
mass_numb = bound_arguments.arguments.pop("mass_numb", None)
self.perform_pre_validations(Z, mass_numb)
processed_kwargs = {
parameter: self.process_argument(parameter, argument, Z, mass_numb)
for parameter, argument in bound_arguments.arguments.items()
}
for parameter in processed_kwargs:
bound_arguments.arguments[parameter] = processed_kwargs[parameter]
return bound_arguments
def particle_input(
callable_: Callable[..., Any] | None = None,
*,
require: str | Iterable[str] | None = None,
any_of: str | Iterable[str] | None = None,
exclude: str | Iterable[str] | None = None,
allow_custom_particles: bool = True,
allow_particle_lists: bool = True,
) -> Callable[..., Any]:
r"""Convert |particle-like| |arguments| into particle objects.
When a callable is |decorated| with |particle_input|,
|particle-like| arguments that are appropriately |annotated| (i.e.,
with |ParticleLike| or |ParticleListLike|) will be converted into a
|Particle|, |CustomParticle|, or |ParticleList|.
The parameters ``Z`` and ``mass_numb`` may be used to specify the
|charge number| of an ion and mass number of an isotope,
respectively, as long as ``Z`` and/or ``mass_numb`` are
|parameters| of the callable and only one parameter is
annotated with |ParticleLike| or |ParticleListLike|.
To indicate that `None` can be passed to a parameter, annotate it
with :py:`ParticleLike | None` or :py:`ParticleListLike | None`.
If the particle representation does not satisfy any categorization
criteria that have been provided, then |particle_input| will raise
an exception.
If the annotated parameter is named ``element``, ``isotope``, or
``ion``, then |particle_input| will raise an exception if the
argument provided to the callable is not consistent with
parameter.
.. note::
An annotated parameter named ``ion`` will accept neutral atoms
and |CustomParticle|\ -like objects as long as the
|charge number| is explicitly defined. To enforce that the
particle be charged, provide :py:`require={"charged"}` to
|particle_input|.
.. note::
When both |particle_input| and |validate_quantities| are used to
decorate a :term:`function`, they may be used in either order.
When using both |particle_input| and |validate_quantities| to
decorate an instance :term:`method`, |particle_input| should be
the outer decorator and |validate_quantities| should be the inner
decorator (see :issue:`2035`).
.. code-block:: python
import astropy.units as u
from plasmapy.particles import particle_input, ParticleLike
from plasmapy.utils.decorators.validators import validate_quantities
class SomeClass:
@particle_input
@validate_quantities
def instance_method(self, particle: ParticleLike, B: u.Quantity[u.T]): ...
.. note::
When decorating a class method with |particle_input|,
`classmethod` should be the outer decorator and |particle_input|
should be the inner decorator, and the first argument
(representing the class) must be named ``cls``.
Parameters
----------
callable_ : callable, optional
The function or method to be decorated.
require : `str` | `set` | `list` | `tuple`, |keyword-only|, optional
Categories that each particle are required to be in.
any_of : `str` | `set` | `list` | `tuple`, |keyword-only|, optional
Categories of which each particle must belong to at least one.
exclude : `str` | `set` | `list` | `tuple`, |keyword-only|, optional
Categories that each particle cannot be in.
allow_custom_particles : bool, |keyword-only|, default: `True`
If `True`, allow |CustomParticle| instances to be passed through.
allow_particle_lists : bool, |keyword-only|, default: `True`
If `True`, allow |ParticleList| instances to be passed through.
Returns
-------
callable
Raises
------
`TypeError`
If the annotated argument is not |particle-like|; or if ``Z`` or
``mass_numb`` is not an integer.
|InvalidParticleError|
If the annotated argument does not correspond to a valid
particle, ``allow_custom_particles`` is `False` and the argument
corresponds to a |CustomParticle|, or ``allow_particle_lists``
is `False` and the argument corresponds to a |ParticleList|.
|InvalidParticleError|
If the decorated argument has charge and/or mass number
information, and ``Z`` and/or ``mass_numb`` contain
contradictory information.
|InvalidElementError|
If an annotated argument is named ``element``, and the input
does not correspond to an element, isotope, or ion.
|InvalidIsotopeError|
If an annotated argument is named ``isotope``, and the input
does not correspond to an isotope or an ion of an isotope.
|InvalidIonError|
If an annotated argument is named ``ion``, and the input does
not correspond to an ion.
|ChargeError|
If ``"charged"`` is in the ``require`` argument and the particle
is not explicitly charged, or if
:py:`any_of = {"charged", "uncharged"}` and the particle does
not have charge information associated with it.
|ParticleError|
If the returned particle(s) do not meet the categorization
criteria specified through ``require``, ``any_of``, or
``exclude``; or if none of the parameters of ``callable_`` have
been appropriately annotated.
`~astropy.units.UnitConversionError`
If the annotated argument is a |Quantity|, but does not have a
physical type of mass or charge.
Warns
-----
: `~plasmapy.particles.exceptions.ParticleWarning`
If decorated argument has charge and/or mass number information,
and ``Z`` and/or ``mass_numb`` contain redundant information.
See Also
--------
~plasmapy.particles.particle_class.CustomParticle
~plasmapy.particles.particle_class.Particle
~plasmapy.particles.particle_collections.ParticleList
~plasmapy.utils.decorators.validators.validate_quantities
Notes
-----
There are some known limitations to |particle_input|.
- Particle categorization criteria are not yet applied to
arguments that get converted into a |ParticleList| (see
:issue:`2048`).
- This decorator is not compatible with setters (see
:issue:`2507`).
- |particle_input| has limited compatibility with positional-only,
variadic positional, and variadic keyword arguments (see
:issue:`2150`).
- When |particle_input| and |validate_quantities| are both
used to decorate an instance method on a class, |particle_input|
must be the outside decorator (see :issue:`2035`).
- Because it dynamically changes arguments, functions decorated with
|particle_input| often do not work well with static type checkers
like mypy. These errors may be silenced by commenting
:py:`# type: ignore[union-attr]` on a line of code, where
``union-attr`` is the name of the mypy error code.
Examples
--------
The |particle_input| decorator takes appropriately annotated
|particle-like| or |particle-list-like| arguments that are provided
to ``callable_``, and converts them into a |Particle|,
|CustomParticle|, or |ParticleList| object.
The following decorated function accepts a |particle-like| input and
returns the corresponding particle object.
>>> from plasmapy.particles import particle_input, ParticleLike
>>> import astropy.units as u
>>> @particle_input
... def get_particle(particle: ParticleLike):
... return particle
>>> get_particle("p+")
Particle("p+")
>>> get_particle(["p+", "e-"])
ParticleList(['p+', 'e-'])
>>> get_particle(1e-26 * u.kg)
CustomParticle(mass=1e-26 kg, charge=nan C)
To allow `None` to pass, use :py:`ParticleLike | None` as the
annotation.
>>> from typing import Optional
>>> @particle_input
... def get_particle_or_none(particle: ParticleLike | None):
... return particle
>>> get_particle_or_none("p+")
Particle("p+")
>>> print(get_particle_or_none(None))
None
If the decorated callable has parameters named ``Z`` and/or
``mass_numb`` and exactly one annotated parameter, then ``Z`` may
be used to provide the |charge number| and ``mass_numb`` may be used
to provide the mass number. Making ``Z`` and ``mass_numb``
|keyword-only| reduces the chances of confusion or mistakes.
>>> @particle_input
... def make_particle(particle: ParticleLike, *, Z=None, mass_numb=None):
... return particle
>>> make_particle("H", Z=0, mass_numb=3)
Particle("T 0+")
Instance methods can also be decorated with |particle_input| as long
as the first argument (representing the instance itself) is named
``self`` following standard convention.
>>> class SampleClass:
... @particle_input
... def __init__(self, particle: ParticleLike):
... self.particle = particle
...
... @particle_input
... def return_particle(self, new_particle: ParticleLike):
... return new_particle
>>> instance = SampleClass("α")
>>> instance.particle
Particle("He-4 2+")
>>> instance.return_particle("T+")
Particle("T 1+")
The ``allow_custom_particles`` and ``allow_particle_lists`` keyword
arguments indicate whether ``callable_`` should accept
|CustomParticle| and |ParticleList| objects, respectively.
>>> @particle_input(allow_custom_particles=False, allow_particle_lists=False)
... def get_atomic_number(particle: ParticleLike):
... return particle.atomic_number
>>> get_atomic_number("Fe")
26
The ``require``, ``any_of``, and ``exclude`` keyword arguments may
be used to specify categorization criteria that a particle must be
consistent with. For more details, please refer to the docstring for
`~plasmapy.particles.particle_class.Particle.is_category`.
>>> @particle_input(require="element", any_of={"charged", "uncharged"})
... def return_ionic_level(particle: ParticleLike):
... return particle
>>> return_ionic_level("Fe-56 0+")
Particle("Fe-56 0+")
When the parameter is named ``element``, ``isotope``, or ``ion``,
then the corresponding argument must be consistent with the name.
When the parameter is named ``ion``, then the particle(s) may also
be a neutral atom as long as the |charge number| is explicitly
defined.
>>> @particle_input
... def mass_number(isotope: ParticleLike):
... return isotope.mass_number
>>> mass_number("D")
2
"""
# The following pattern comes from the docs for wrapt, and requires
# that the arguments to the decorator are keyword-only.
if callable_ is None:
return functools.partial(
particle_input,
require=require,
any_of=any_of,
exclude=exclude,
allow_custom_particles=allow_custom_particles,
allow_particle_lists=allow_particle_lists,
)
particle_validator = _ParticleInput(
callable_=callable_,
require=require,
any_of=any_of,
exclude=exclude,
allow_custom_particles=allow_custom_particles,
allow_particle_lists=allow_particle_lists,
)
@wrapt.decorator
def wrapper(
callable__: Callable[..., Any],
instance: Any,
args: Iterable[Any],
kwargs: MutableMapping[str, Any],
) -> Callable[..., Any]:
bound_arguments = particle_validator.process_arguments(args, kwargs, instance)
return callable__( # type: ignore[no-any-return]
*bound_arguments.args,
**bound_arguments.kwargs,
)
return wrapper(callable_, instance=None, args=(), kwargs={})