44from typing import (
55 TYPE_CHECKING ,
66 Any ,
7- ClassVar ,
87 Literal ,
98 cast ,
109)
@@ -114,9 +113,12 @@ class StringDtype(StorageExtensionDtype):
114113 string[pyarrow]
115114 """
116115
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"
120122
121123 #: StringDtype().na_value uses pandas.NA except the implementation that
122124 # follows NumPy semantics, which uses nan.
@@ -133,7 +135,7 @@ def __init__(
133135 ) -> None :
134136 # infer defaults
135137 if storage is None :
136- if using_string_dtype () and na_value is not libmissing .NA :
138+ if na_value is not libmissing .NA :
137139 if HAS_PYARROW :
138140 storage = "pyarrow"
139141 else :
@@ -166,11 +168,19 @@ def __init__(
166168 self .storage = storage
167169 self ._na_value = na_value
168170
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+
169178 def __eq__ (self , other : object ) -> bool :
170179 # we need to override the base class __eq__ because na_value (NA or NaN)
171180 # cannot be checked with normal `==`
172181 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
174184 return True
175185 try :
176186 other = self .construct_from_string (other )
@@ -227,6 +237,8 @@ def construct_from_string(cls, string) -> Self:
227237 )
228238 if string == "string" :
229239 return cls ()
240+ elif string == "str" and using_string_dtype ():
241+ return cls (na_value = np .nan )
230242 elif string == "string[python]" :
231243 return cls (storage = "python" )
232244 elif string == "string[pyarrow]" :
0 commit comments