Towards a unique AgentSet class #12
adamamer20
started this conversation in
Ideas
Replies: 2 comments 5 replies
-
How does pandas do it currently? They used to have an optional PyArrow backend (https://github.com/search?q=repo%3Apandas-dev%2Fpandas%20pyarrow&type=code), but required since PDEP-10 (but I haven't seen any PR that makes PyArrow to be the sole backend). I will post my findings here. There is also PDEP-13 (pandas logical type system). |
Beta Was this translation helpful? Give feedback.
5 replies
-
This discussion becomes even more relevant for GridDF, which could be a generic on DF and Series if we expect users to not subclass the grids classes. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Overview
In Python, type hints are static and do not support dynamic type inference. To have a single type-safe AgentSet class, one must be able to define types based on the backend class attribute. A solution is to use a generic class on the dataframe/series type. However, it's currently challenging to maintain type-safe, generic classes across inheritance hierarchies. Specifically, it's currently not possible to return
Self
with generic types.Current Limitation
The static nature of Python's type hints means that when dealing with generic types, we cannot dynamically infer the correct type in subclass methods that are intended to return instances of the subclass. The only way to specify dynamic return types is through the use of generic type variables. However, Python's type hinting system does not yet support returning
Self[Generics]
.Example 1: Working with Factory Functions Returning
AgentSetDF
In this example, we demonstrate how to use a factory function to return instances of the
AgentSetDF
class with generic type hints. This approach works because the factory function explicitly returnsAgentSetDF
.Example 2: Failing with Factory Functions Returning
Self
In this example, we attempt to use factory functions that return
Self
. This approach does not work because Python's type hinting system does not support returningSelf
with generic types, making it impossible to keep generic types across inheritance.Conclusion
Due to the static nature of Python's type hints, it is currently not possible to dynamically infer types using
Self
with generics. The only way to achieve type-safe factory functions is to return the class type directly, as shown in Example 1. However, this approach has its limitations and can become cumbersome when dealing with more complex inheritance hierarchies. Type hinting in python is a relatively new feature, introduced only in Python 3.12. In the future, a solution might be possible.For more details, refer to the following Stack Overflow discussion: Typing hint for abstract class method that returns class instance.
Beta Was this translation helpful? Give feedback.
All reactions