77import numpy as np
88import pandas as pd
99from pandas import Categorical
10- from pandas .api .typing .aliases import (
11- Ordered ,
12- )
10+ from pandas .api .typing .aliases import Ordered
1311from pandas .core .arrays .categorical import CategoricalDtype
1412from pandas .core .indexes .base import Index
1513
1917from tests ._typing import (
2018 np_1darray ,
2119 np_1darray_bool ,
20+ np_ndarray_str ,
2221)
2322
2423
@@ -53,48 +52,45 @@ def test_constructor() -> None:
5352 cat = Categorical (dd )
5453 check (assert_type (cat , "Categorical[str]" ), Categorical , str )
5554
56- values = np . array ([ "a" , "b" , "c" , "a" ])
57- cat_np = Categorical ( values )
58- # np.array() is typed as ndarray[Any, Any] by numpy stubs, so mypy cannot infer
59- # the element type; the actual type is Categorical[str]
60- # TODO: facebook/pyrefly#0
61- check (assert_type (cat_np , "Categorical[str]" ), Categorical ) # type: ignore[assert-type] # pyrefly: ignore[assert-type]
55+ values = check (
56+ assert_type ( np . array ([ "a" , "b" , "c" , "a" ], np . str_ ), np_ndarray_str ),
57+ np_1darray ,
58+ np . str_ ,
59+ )
60+ check (assert_type (Categorical ( values ) , "Categorical[str]" ), Categorical , str )
6261
6362 cat = Categorical (["a" , "b" , "c" ], categories = ["a" , "b" , "c" , "d" ])
64- check (assert_type (cat , "Categorical[str]" ), Categorical )
63+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
6564
66- cat = Categorical ([ "a" , "b" , "c" ], categories = np . array ([ "a" , "b" , "c" , "d" ]))
67- # TODO: facebook/pyrefly#0
68- # pyrefly: ignore[assert-type]
69- check (assert_type (cat , "Categorical[str]" ), Categorical )
65+ cat = Categorical (
66+ [ "a" , "b" , "c" ], categories = np . array ([ "a" , "b" , "c" , "d" ], np . str_ )
67+ )
68+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
7069
7170 cat = Categorical (["a" , "b" , "c" ], categories = ["a" , "b" , "c" ], ordered = True )
72- check (assert_type (cat , "Categorical[str]" ), Categorical )
71+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
7372
7473 cat = Categorical (["a" , "b" , "c" ], categories = ["a" , "b" , "c" ], ordered = False )
75- check (assert_type (cat , "Categorical[str]" ), Categorical )
74+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
7675
7776 cat = Categorical (["a" , "b" , "c" ], categories = ["a" , "b" , "c" ], ordered = None )
78- check (assert_type (cat , "Categorical[str]" ), Categorical )
77+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
7978
8079 cat = Categorical (
81- values = ["x" , "y" , "z" , "x" ],
82- categories = ["x" , "y" , "z" ],
83- ordered = True ,
84- copy = True ,
80+ values = ["x" , "y" , "z" , "x" ], categories = ["x" , "y" , "z" ], ordered = True , copy = True
8581 )
86- check (assert_type (cat , "Categorical[str]" ), Categorical )
82+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
8783
8884 dtype = pd .CategoricalDtype (categories = ["x" , "y" , "z" ], ordered = True )
8985 cat = Categorical (
9086 values = ["x" , "y" , "z" , "x" ],
9187 dtype = dtype ,
9288 copy = True ,
9389 )
94- check (assert_type (cat , "Categorical[str]" ), Categorical )
90+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
9591
9692 cat_int = Categorical ([1 , 2 , 3 , 1 , 2 ])
97- check (assert_type (cat_int , "Categorical[int]" ), Categorical )
93+ check (assert_type (cat_int , "Categorical[int]" ), Categorical , int )
9894
9995 cat_mixed = Categorical (["a" , 1 , "b" , 2 ])
10096 check (assert_type (cat_mixed , Categorical ), Categorical )
@@ -103,19 +99,19 @@ def test_constructor() -> None:
10399 check (assert_type (cat_empty , Categorical ), Categorical )
104100
105101 cat = Categorical (["a" , "b" , "c" ], categories = None )
106- check (assert_type (cat , "Categorical[str]" ), Categorical )
102+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
107103
108104 cat1 = Categorical (["a" , "b" , "c" ])
109105 cat = Categorical (cat1 )
110- check (assert_type (cat , "Categorical[str]" ), Categorical )
106+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
111107
112108 values_series = pd .Series (["a" , "b" , "c" , "a" ])
113109 cat = Categorical (values_series )
114- check (assert_type (cat , "Categorical[str]" ), Categorical )
110+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
115111
116112 values_index = pd .Index (["a" , "b" , "c" , "a" ])
117113 cat = Categorical (values_index )
118- check (assert_type (cat , "Categorical[str]" ), Categorical )
114+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
119115
120116
121117def test_categorical_dtype () -> None :
0 commit comments