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
@@ -60,54 +59,45 @@ def test_constructor() -> None:
6059 cat = Categorical (dd )
6160 check (assert_type (cat , "Categorical[str]" ), Categorical , str )
6261
63- values = np . array ([ "a" , "b" , "c" , "a" ])
64- cat_np = Categorical ( values )
65- # np.array() is typed as ndarray[Any, Any] by numpy stubs, so mypy cannot infer
66- # the element type; the actual type is Categorical[str]
67- # TODO: https://github.com/facebook/pyrefly/issues/3891
68- check (assert_type (cat_np , "Categorical[str]" ), Categorical ) # type: ignore[assert-type] # pyrefly: ignore[assert-type]
62+ values = check (
63+ assert_type ( np . array ([ "a" , "b" , "c" , "a" ], np . str_ ), np_ndarray_str ),
64+ np_1darray ,
65+ np . str_ ,
66+ )
67+ check (assert_type (Categorical ( values ) , "Categorical[str]" ), Categorical , str )
6968
7069 cat = Categorical (["a" , "b" , "c" ], categories = ["a" , "b" , "c" , "d" ])
71- check (assert_type (cat , "Categorical[str]" ), Categorical )
70+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
7271
73- cat = Categorical (["a" , "b" , "c" ], categories = np .array (["a" , "b" , "c" , "d" ]))
74- # TODO: https://github.com/facebook/pyrefly/issues/3891
75- check (
76- assert_type (cat , "Categorical[str]" ), # pyrefly: ignore[assert-type]
77- Categorical ,
72+ cat = Categorical (
73+ ["a" , "b" , "c" ], categories = np .array (["a" , "b" , "c" , "d" ], np .str_ )
7874 )
75+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
7976
8077 cat = Categorical (["a" , "b" , "c" ], categories = ["a" , "b" , "c" ], ordered = True )
81- check (assert_type (cat , "Categorical[str]" ), Categorical )
78+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
8279
8380 cat = Categorical (["a" , "b" , "c" ], categories = ["a" , "b" , "c" ], ordered = False )
84- check (assert_type (cat , "Categorical[str]" ), Categorical )
81+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
8582
8683 cat = Categorical (["a" , "b" , "c" ], categories = ["a" , "b" , "c" ], ordered = None )
87- check (assert_type (cat , "Categorical[str]" ), Categorical )
84+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
8885
8986 cat = Categorical (
90- values = ["x" , "y" , "z" , "x" ],
91- categories = ["x" , "y" , "z" ],
92- ordered = True ,
93- copy = True ,
87+ values = ["x" , "y" , "z" , "x" ], categories = ["x" , "y" , "z" ], ordered = True , copy = True
9488 )
95- check (assert_type (cat , "Categorical[str]" ), Categorical )
89+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
9690
9791 dtype = pd .CategoricalDtype (categories = ["x" , "y" , "z" ], ordered = True )
9892 cat = Categorical (
9993 values = ["x" , "y" , "z" , "x" ],
10094 dtype = dtype ,
10195 copy = True ,
10296 )
103- check (assert_type (cat , "Categorical[str]" ), Categorical )
97+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
10498
10599 cat_int = Categorical ([1 , 2 , 3 , 1 , 2 ])
106- # TODO: https://github.com/facebook/pyrefly/issues/3891
107- check (
108- assert_type (cat_int , "Categorical[int]" ), # pyrefly: ignore[assert-type]
109- Categorical ,
110- )
100+ check (assert_type (cat_int , "Categorical[int]" ), Categorical , int )
111101
112102 cat_mixed = Categorical (["a" , 1 , "b" , 2 ])
113103 # TODO: https://github.com/facebook/pyrefly/issues/3891
@@ -119,19 +109,19 @@ def test_constructor() -> None:
119109 check (assert_type (cat_empty , Categorical ), Categorical )
120110
121111 cat = Categorical (["a" , "b" , "c" ], categories = None )
122- check (assert_type (cat , "Categorical[str]" ), Categorical )
112+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
123113
124114 cat1 = Categorical (["a" , "b" , "c" ])
125115 cat = Categorical (cat1 )
126- check (assert_type (cat , "Categorical[str]" ), Categorical )
116+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
127117
128118 values_series = pd .Series (["a" , "b" , "c" , "a" ])
129119 cat = Categorical (values_series )
130- check (assert_type (cat , "Categorical[str]" ), Categorical )
120+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
131121
132122 values_index = pd .Index (["a" , "b" , "c" , "a" ])
133123 cat = Categorical (values_index )
134- check (assert_type (cat , "Categorical[str]" ), Categorical )
124+ check (assert_type (cat , "Categorical[str]" ), Categorical , str )
135125
136126
137127def test_categorical_dtype () -> None :
0 commit comments