99import types
1010from typing import Any , Callable , Iterable , Optional , Tuple
1111
12- import geopandas
1312import numpy as np
1413import pandas as pd
1514import polars as pl
1615import pytest
17- import torch
1816from fastcore .foundation import L
19- from shapely .geometry import Polygon
2017
2118from positron import inspectors
2219from positron .inspectors import _get_simplified_qualname , get_inspector
3734)
3835from .utils import get_type_as_str
3936
37+ try :
38+ import geopandas
39+ from shapely .geometry import Polygon
40+ except ImportError :
41+ geopandas = None
42+ Polygon = None
43+
44+ try :
45+ import torch
46+ except ImportError :
47+ torch = None
48+
49+ try :
50+ import ibis
51+ except ImportError :
52+ ibis = None
53+
4054
4155def verify_inspector (
4256 * ,
@@ -627,6 +641,7 @@ def mutate(x):
627641 )
628642
629643
644+ @pytest .mark .skipif (geopandas is None , reason = "geopandas is not available" )
630645def test_inspect_geopandas_dataframe () -> None :
631646 p1 = Polygon ([(0 , 0 ), (1 , 0 ), (1 , 1 )])
632647 p2 = Polygon ([(0 , 0 ), (1 , 0 ), (1 , 1 ), (0 , 1 )])
@@ -708,19 +723,15 @@ def mutate(x):
708723 )
709724
710725
711- @pytest .mark .parametrize (
712- "value" ,
713- [
714- geopandas .GeoSeries (
715- [
716- Polygon ([(0 , 0 ), (1 , 0 ), (1 , 1 )]),
717- Polygon ([(0 , 0 ), (1 , 0 ), (1 , 1 ), (0 , 1 )]),
718- Polygon ([(2 , 0 ), (3 , 0 ), (3 , 1 ), (2 , 1 )]),
719- ]
720- ),
721- ],
722- )
723- def test_inspect_geopandas_series (value : geopandas .GeoSeries ) -> None :
726+ @pytest .mark .skipif (geopandas is None , reason = "geopandas is not available" )
727+ def test_inspect_geopandas_series () -> None :
728+ value = geopandas .GeoSeries (
729+ [
730+ Polygon ([(0 , 0 ), (1 , 0 ), (1 , 1 )]),
731+ Polygon ([(0 , 0 ), (1 , 0 ), (1 , 1 ), (0 , 1 )]),
732+ Polygon ([(2 , 0 ), (3 , 0 ), (3 , 1 ), (2 , 1 )]),
733+ ]
734+ )
724735 (rows ,) = value .shape
725736
726737 def mutate (x ):
@@ -875,9 +886,8 @@ def test_get_child(value: Any, key: Any, expected: Any) -> None:
875886
876887
877888@pytest .mark .skipif (sys .version_info < (3 , 10 ), reason = "requires Python 3.10 or higher" )
889+ @pytest .mark .skipif (ibis is None , reason = "ibis not available" )
878890def test_inspect_ibis_exprs () -> None :
879- import ibis
880-
881891 # Make sure we don't return an executed repr
882892 ibis .options .interactive = True
883893
@@ -921,16 +931,21 @@ def test_inspect_ibis_exprs() -> None:
921931@pytest .mark .parametrize (
922932 ("value" , "expected" ),
923933 [
924- (np .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]], dtype = "int64" ), 48 ),
925- (torch .Tensor ([[1 , 2 , 3 ], [4 , 5 , 6 ]]), 24 ),
926- (pd .Series ([1 , 2 , 3 , 4 ]), 32 ),
927- (pl .Series ([1 , 2 , 3 , 4 ]), 32 ),
928- (pd .DataFrame ({"a" : [1 , 2 ], "b" : ["3" , "4" ]}), 4 ),
929- (pl .DataFrame ({"a" : [1 , 2 ], "b" : ["3" , "4" ]}), 4 ),
930- (pd .Index ([0 , 1 ]), 16 ),
934+ (lambda : np .array ([[1 , 2 , 3 ], [4 , 5 , 6 ]], dtype = "int64" ), 48 ),
935+ pytest .param (
936+ lambda : torch .Tensor ([[1 , 2 , 3 ], [4 , 5 , 6 ]]),
937+ 24 ,
938+ marks = pytest .mark .skipif (torch is None , reason = "torch not available" ),
939+ ),
940+ (lambda : pd .Series ([1 , 2 , 3 , 4 ]), 32 ),
941+ (lambda : pl .Series ([1 , 2 , 3 , 4 ]), 32 ),
942+ (lambda : pd .DataFrame ({"a" : [1 , 2 ], "b" : ["3" , "4" ]}), 4 ),
943+ (lambda : pl .DataFrame ({"a" : [1 , 2 ], "b" : ["3" , "4" ]}), 4 ),
944+ (lambda : pd .Index ([0 , 1 ]), 16 ),
931945 ],
932946)
933- def test_arrays_maps_get_size (value : Any , expected : int ) -> None :
947+ def test_arrays_maps_get_size (value : Callable , expected : int ) -> None :
948+ value = value ()
934949 if value is None :
935950 return
936951 inspector = get_inspector (value )
@@ -944,43 +959,53 @@ class VeryLongClassNameThatShouldDefinitelyBeTruncatedBecauseItIsWayTooLong:
944959@pytest .mark .parametrize (
945960 "value" ,
946961 [
947- pytest .param ("The quick brown fox jumps over the lazy dog" , id = "string" ),
948- pytest .param (sys .maxsize * 100 , id = "int" ),
949- pytest .param (sys .float_info .max , id = "float" ),
950- pytest .param (complex (sys .float_info .min , sys .float_info .max ), id = "complex" ),
962+ pytest .param (lambda : "The quick brown fox jumps over the lazy dog" , id = "string" ),
963+ pytest .param (lambda : sys .maxsize * 100 , id = "int" ),
964+ pytest .param (lambda : sys .float_info .max , id = "float" ),
965+ pytest .param (lambda : complex (sys .float_info .min , sys .float_info .max ), id = "complex" ),
951966 pytest .param (
952- VeryLongClassNameThatShouldDefinitelyBeTruncatedBecauseItIsWayTooLong , id = "class"
967+ lambda : VeryLongClassNameThatShouldDefinitelyBeTruncatedBecauseItIsWayTooLong ,
968+ id = "class" ,
953969 ),
954- pytest .param (b"The quick brown fox jumps over the lazy dog" , id = "bytes" ),
955- pytest .param (bytearray (b"The quick brown fox jumps over the lazy dog" ), id = "bytearray" ),
956- pytest .param (set (range (20 )), id = "set" ),
957- pytest .param (frozenset (range (20 )), id = "frozenset" ),
958- pytest .param (list (range (20 )), id = "list" ),
959- pytest .param (LIST_WITH_CYCLE , id = "list_cycle" ),
960- pytest .param (range (12345678901 ), id = "range" ),
961- pytest .param (L (range (20 )), id = "fastcore_list" ),
962- pytest .param (FASTCORE_LIST_WITH_CYCLE , id = "fastcore_list_cycle" ),
963- pytest .param ({str (i ): i for i in range (20 )}, id = "map" ),
964- pytest .param (MAP_WITH_CYCLE , id = "map_cycle" ),
970+ pytest .param (lambda : b"The quick brown fox jumps over the lazy dog" , id = "bytes" ),
965971 pytest .param (
966- datetime .datetime (2021 , 1 , 1 , 1 , 23 , 45 , tzinfo = datetime .timezone .utc ),
972+ lambda : bytearray (b"The quick brown fox jumps over the lazy dog" ), id = "bytearray"
973+ ),
974+ pytest .param (lambda : set (range (20 )), id = "set" ),
975+ pytest .param (lambda : frozenset (range (20 )), id = "frozenset" ),
976+ pytest .param (lambda : list (range (20 )), id = "list" ),
977+ pytest .param (lambda : LIST_WITH_CYCLE , id = "list_cycle" ),
978+ pytest .param (lambda : range (12345678901 ), id = "range" ),
979+ pytest .param (lambda : L (range (20 )), id = "fastcore_list" ),
980+ pytest .param (lambda : FASTCORE_LIST_WITH_CYCLE , id = "fastcore_list_cycle" ),
981+ pytest .param (lambda : {str (i ): i for i in range (20 )}, id = "map" ),
982+ pytest .param (lambda : MAP_WITH_CYCLE , id = "map_cycle" ),
983+ pytest .param (
984+ lambda : datetime .datetime (2021 , 1 , 1 , 1 , 23 , 45 , tzinfo = datetime .timezone .utc ),
967985 id = "timestamp_datetime" ,
968986 ),
969- pytest .param (pd .Timestamp ("2021-01-01 01:23:45" ), id = "timestamp_pandas" ),
970- pytest .param (pd .Index (list (range (20 ))), id = "pandas_index" ),
971- pytest .param (pd .Series (list (range (20 ))), id = "pandas_series" ),
987+ pytest .param (lambda : pd .Timestamp ("2021-01-01 01:23:45" ), id = "timestamp_pandas" ),
988+ pytest .param (lambda : pd .Index (list (range (20 ))), id = "pandas_index" ),
989+ pytest .param (lambda : pd .Series (list (range (20 ))), id = "pandas_series" ),
990+ pytest .param (
991+ lambda : pd .DataFrame ({"a" : list (range (20 )), "b" : list (range (20 ))}),
992+ id = "pandas_dataframe" ,
993+ ),
994+ pytest .param (lambda : pl .Series (list (range (20 ))), id = "polars_series" ),
972995 pytest .param (
973- pd .DataFrame ({"a" : list (range (20 )), "b" : list (range (20 ))}), id = "pandas_dataframe"
996+ lambda : pl .DataFrame ({"a" : list (range (20 )), "b" : list (range (20 ))}),
997+ id = "polars_dataframe" ,
974998 ),
975- pytest .param (pl . Series ( list ( range ( 20 ))) , id = "polars_series " ),
999+ pytest .param (lambda : np . ones (( 20 , 20 )) , id = "numpy_array " ),
9761000 pytest .param (
977- pl .DataFrame ({"a" : list (range (20 )), "b" : list (range (20 ))}), id = "polars_dataframe"
1001+ lambda : torch .ones ((20 , 20 )),
1002+ id = "torch_tensor" ,
1003+ marks = pytest .mark .skipif (torch is None , reason = "torch not available" ),
9781004 ),
979- pytest .param (np .ones ((20 , 20 )), id = "numpy_array" ),
980- pytest .param (torch .ones ((20 , 20 )), id = "torch_tensor" ),
9811005 ],
9821006)
9831007def test_truncated_display_value (value , snapshot , monkeypatch ) -> None :
1008+ value = value ()
9841009 # Patch the maximum string length for faster and more readable tests.
9851010 monkeypatch .setattr (inspectors , "MAX_ITEMS_BY_LEVEL" , (20 , 10 ))
9861011 monkeypatch .setattr (inspectors , "MAX_CHARACTERS" , 20 )
0 commit comments