File: py3_test_quantity_annotations.py

package info (click to toggle)
python-astropy 1.3-8~bpo8%2B2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 44,292 kB
  • sloc: ansic: 160,360; python: 137,322; sh: 11,493; lex: 7,638; yacc: 4,956; xml: 1,796; makefile: 474; cpp: 364
file content (229 lines) | stat: -rw-r--r-- 6,031 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst

from functools import wraps
from textwrap import dedent

from ... import units as u  # pylint: disable=W0611
from ...extern import six
from ...tests.helper import pytest


def py3only(func):
    if six.PY2:
        return pytest.mark.skipif('six.PY2')(func)
    else:
        @wraps(func)
        def wrapper(*args, **kwargs):
            if func.__doc__ is None:
                pytest.skip('unable to run this test due to missing '
                            'docstrings (maybe the module was compiled with '
                            'optimization flags?)')

            code = compile(dedent(func.__doc__), __file__, 'exec')
            # This uses an unqualified exec statement illegally in Python 2,
            # but perfectly allowed in Python 3 so in fact we eval the exec
            # call :)
            eval('exec(code)')

        return wrapper


@py3only
def test_args3():
    """
    @u.quantity_input
    def myfunc_args(solarx: u.arcsec, solary: u.arcsec):
        return solarx, solary

    solarx, solary = myfunc_args(1*u.arcsec, 1*u.arcsec)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, u.Quantity)

    assert solarx.unit == u.arcsec
    assert solary.unit == u.arcsec
    """


@py3only
def test_args_noconvert3():
    """
    @u.quantity_input()
    def myfunc_args(solarx: u.arcsec, solary: u.arcsec):
        return solarx, solary

    solarx, solary = myfunc_args(1*u.deg, 1*u.arcmin)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, u.Quantity)

    assert solarx.unit == u.deg
    assert solary.unit == u.arcmin
    """


@py3only
def test_args_nonquantity3():
    """
    @u.quantity_input
    def myfunc_args(solarx: u.arcsec, solary):
        return solarx, solary

    solarx, solary = myfunc_args(1*u.arcsec, 100)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, int)

    assert solarx.unit == u.arcsec
    """


@py3only
def test_arg_equivalencies3():
    """
    @u.quantity_input(equivalencies=u.mass_energy())
    def myfunc_args(solarx: u.arcsec, solary: u.eV):
        return solarx, solary+(10*u.J)  # Add an energy to check equiv is working

    solarx, solary = myfunc_args(1*u.arcsec, 100*u.gram)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, u.Quantity)

    assert solarx.unit == u.arcsec
    assert solary.unit == u.gram
    """


@py3only
def test_wrong_unit3():
    """
    @u.quantity_input
    def myfunc_args(solarx: u.arcsec, solary: u.deg):
        return solarx, solary

    with pytest.raises(u.UnitsError) as e:
        solarx, solary = myfunc_args(1*u.arcsec, 100*u.km)
    assert str(e.value) == "Argument 'solary' to function 'myfunc_args' must be in units convertible to 'deg'."
    """


@py3only
def test_not_quantity3():
    """
    @u.quantity_input
    def myfunc_args(solarx: u.arcsec, solary: u.deg):
        return solarx, solary

    with pytest.raises(TypeError) as e:
        solarx, solary = myfunc_args(1*u.arcsec, 100)
    assert str(e.value) == "Argument 'solary' to function 'myfunc_args' has no 'unit' attribute. You may want to pass in an astropy Quantity instead."
    """


@py3only
def test_decorator_override():
    """
    @u.quantity_input(solarx=u.arcsec)
    def myfunc_args(solarx: u.km, solary: u.arcsec):
        return solarx, solary

    solarx, solary = myfunc_args(1*u.arcsec, 1*u.arcsec)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, u.Quantity)

    assert solarx.unit == u.arcsec
    assert solary.unit == u.arcsec
    """


@py3only
def test_kwargs3():
    """
    @u.quantity_input
    def myfunc_args(solarx: u.arcsec, solary, myk: u.arcsec=1*u.arcsec):
        return solarx, solary, myk

    solarx, solary, myk = myfunc_args(1*u.arcsec, 100, myk=100*u.deg)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, int)
    assert isinstance(myk, u.Quantity)

    assert myk.unit == u.deg
    """


@py3only
def test_unused_kwargs3():
    """
    @u.quantity_input
    def myfunc_args(solarx: u.arcsec, solary, myk: u.arcsec=1*u.arcsec, myk2=1000):
        return solarx, solary, myk, myk2

    solarx, solary, myk, myk2 = myfunc_args(1*u.arcsec, 100, myk=100*u.deg, myk2=10)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, int)
    assert isinstance(myk, u.Quantity)
    assert isinstance(myk2, int)

    assert myk.unit == u.deg
    assert myk2 == 10
    """


@py3only
def test_kwarg_equivalencies3():
    """
    @u.quantity_input(equivalencies=u.mass_energy())
    def myfunc_args(solarx: u.arcsec, energy: u.eV=10*u.eV):
        return solarx, energy+(10*u.J)  # Add an energy to check equiv is working

    solarx, energy = myfunc_args(1*u.arcsec, 100*u.gram)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(energy, u.Quantity)

    assert solarx.unit == u.arcsec
    assert energy.unit == u.gram
    """


@py3only
def test_kwarg_wrong_unit3():
    """
    @u.quantity_input
    def myfunc_args(solarx: u.arcsec, solary: u.deg=10*u.deg):
        return solarx, solary

    with pytest.raises(u.UnitsError) as e:
        solarx, solary = myfunc_args(1*u.arcsec, solary=100*u.km)
    assert str(e.value) == "Argument 'solary' to function 'myfunc_args' must be in units convertible to 'deg'."
    """


@py3only
def test_kwarg_not_quantity3():
    """
    @u.quantity_input
    def myfunc_args(solarx: u.arcsec, solary: u.deg=10*u.deg):
        return solarx, solary

    with pytest.raises(TypeError) as e:
        solarx, solary = myfunc_args(1*u.arcsec, solary=100)
    assert str(e.value) == "Argument 'solary' to function 'myfunc_args' has no 'unit' attribute. You may want to pass in an astropy Quantity instead."
    """


@py3only
def test_kwarg_default3():
    """
    @u.quantity_input
    def myfunc_args(solarx: u.arcsec, solary: u.deg=10*u.deg):
        return solarx, solary

    solarx, solary = myfunc_args(1*u.arcsec)
    """