11"""testing stancsv parsing"""
22
3+ from test import without_import
34from typing import List
4- from unittest import mock
55
66import numpy as np
77import pytest
88
9+ import cmdstanpy
910from cmdstanpy .utils import stancsv
1011
1112
@@ -46,7 +47,7 @@ def test_csv_bytes_to_numpy_no_header_no_polars():
4647 ],
4748 dtype = np .float64 ,
4849 )
49- with mock . patch . dict ( "sys.modules " , { "polars" : None } ):
50+ with without_import ( "polars " , cmdstanpy . utils . stancsv ):
5051 arr_out = stancsv .csv_bytes_list_to_numpy (lines , includes_header = False )
5152 assert np .array_equal (arr_out , expected )
5253 assert arr_out [0 ].dtype == np .float64
@@ -100,7 +101,7 @@ def test_csv_bytes_to_numpy_single_element_no_polars():
100101 ],
101102 dtype = np .float64 ,
102103 )
103- with mock . patch . dict ( "sys.modules " , { "polars" : None } ):
104+ with without_import ( "polars " , cmdstanpy . utils . stancsv ):
104105 arr_out = stancsv .csv_bytes_list_to_numpy (lines , includes_header = False )
105106 assert np .array_equal (arr_out , expected )
106107
@@ -125,7 +126,7 @@ def test_csv_bytes_to_numpy_with_header_no_polars():
125126 ],
126127 dtype = np .float64 ,
127128 )
128- with mock . patch . dict ( "sys.modules " , { "polars" : None } ):
129+ with without_import ( "polars " , cmdstanpy . utils . stancsv ):
129130 arr_out = stancsv .csv_bytes_list_to_numpy (lines , includes_header = True )
130131 assert np .array_equal (arr_out , expected )
131132
@@ -138,7 +139,7 @@ def test_csv_bytes_to_numpy_empty():
138139
139140def test_csv_bytes_to_numpy_empty_no_polars ():
140141 lines = [b"" ]
141- with mock . patch . dict ( "sys.modules " , { "polars" : None } ):
142+ with without_import ( "polars " , cmdstanpy . utils . stancsv ):
142143 with pytest .raises (ValueError ):
143144 stancsv .csv_bytes_list_to_numpy (lines )
144145
@@ -161,7 +162,7 @@ def test_csv_bytes_to_numpy_header_no_draws_no_polars():
161162 b"n_leapfrog__,divergent__,energy__,theta\n "
162163 ),
163164 ]
164- with mock . patch . dict ( "sys.modules " , { "polars" : None } ):
165+ with without_import ( "polars " , cmdstanpy . utils . stancsv ):
165166 with pytest .raises (ValueError ):
166167 stancsv .csv_bytes_list_to_numpy (lines )
167168
@@ -272,7 +273,7 @@ def test_csv_polars_and_numpy_equiv():
272273 arr_out_polars = stancsv .csv_bytes_list_to_numpy (
273274 lines , includes_header = False
274275 )
275- with mock . patch . dict ( "sys.modules " , { "polars" : None } ):
276+ with without_import ( "polars " , cmdstanpy . utils . stancsv ):
276277 arr_out_numpy = stancsv .csv_bytes_list_to_numpy (
277278 lines , includes_header = False
278279 )
@@ -286,7 +287,7 @@ def test_csv_polars_and_numpy_equiv_one_line():
286287 arr_out_polars = stancsv .csv_bytes_list_to_numpy (
287288 lines , includes_header = False
288289 )
289- with mock . patch . dict ( "sys.modules " , { "polars" : None } ):
290+ with without_import ( "polars " , cmdstanpy . utils . stancsv ):
290291 arr_out_numpy = stancsv .csv_bytes_list_to_numpy (
291292 lines , includes_header = False
292293 )
@@ -300,7 +301,7 @@ def test_csv_polars_and_numpy_equiv_one_element():
300301 arr_out_polars = stancsv .csv_bytes_list_to_numpy (
301302 lines , includes_header = False
302303 )
303- with mock . patch . dict ( "sys.modules " , { "polars" : None } ):
304+ with without_import ( "polars " , cmdstanpy . utils . stancsv ):
304305 arr_out_numpy = stancsv .csv_bytes_list_to_numpy (
305306 lines , includes_header = False
306307 )
0 commit comments