4
4
from typing import (
5
5
TYPE_CHECKING ,
6
6
Any ,
7
- ClassVar ,
8
7
Literal ,
9
8
cast ,
10
9
)
@@ -114,9 +113,12 @@ class StringDtype(StorageExtensionDtype):
114
113
string[pyarrow]
115
114
"""
116
115
117
- # error: Cannot override instance variable (previously declared on
118
- # base class "StorageExtensionDtype") with class variable
119
- name : ClassVar [str ] = "string" # type: ignore[misc]
116
+ @property
117
+ def name (self ) -> str : # type: ignore[override]
118
+ if self ._na_value is libmissing .NA :
119
+ return "string"
120
+ else :
121
+ return "str"
120
122
121
123
#: StringDtype().na_value uses pandas.NA except the implementation that
122
124
# follows NumPy semantics, which uses nan.
@@ -133,7 +135,7 @@ def __init__(
133
135
) -> None :
134
136
# infer defaults
135
137
if storage is None :
136
- if using_string_dtype () and na_value is not libmissing .NA :
138
+ if na_value is not libmissing .NA :
137
139
if HAS_PYARROW :
138
140
storage = "pyarrow"
139
141
else :
@@ -166,11 +168,19 @@ def __init__(
166
168
self .storage = storage
167
169
self ._na_value = na_value
168
170
171
+ def __repr__ (self ) -> str :
172
+ if self ._na_value is libmissing .NA :
173
+ return f"{ self .name } [{ self .storage } ]"
174
+ else :
175
+ # TODO add more informative repr
176
+ return self .name
177
+
169
178
def __eq__ (self , other : object ) -> bool :
170
179
# we need to override the base class __eq__ because na_value (NA or NaN)
171
180
# cannot be checked with normal `==`
172
181
if isinstance (other , str ):
173
- if other == self .name :
182
+ # TODO should dtype == "string" work for the NaN variant?
183
+ if other == "string" or other == self .name : # noqa: PLR1714
174
184
return True
175
185
try :
176
186
other = self .construct_from_string (other )
@@ -227,6 +237,8 @@ def construct_from_string(cls, string) -> Self:
227
237
)
228
238
if string == "string" :
229
239
return cls ()
240
+ elif string == "str" and using_string_dtype ():
241
+ return cls (na_value = np .nan )
230
242
elif string == "string[python]" :
231
243
return cls (storage = "python" )
232
244
elif string == "string[pyarrow]" :
0 commit comments