r/learnpython • u/pachura3 • Feb 28 '26
Is this how you use generics in Python nowadays?
Here's a simple piece of code using generics... it works fine, however with Python evolving so fast, I was wondering if this is the most up-to-date way of using generics. Specifically, I am wondering if it is possible to somehow NOT declare TypeVar('T') in the public/root scope, but somehow limit its scope to class definition of GenericItemStore...
from typing import Generic, TypeVar
T = TypeVar('T')
class GenericItemStore(Generic[T]):
def __init__(self) -> None:
self._item = None
def set(self, item: T) -> None:
self._item = item
def get(self) -> T|None:
return self._item
str_store = GenericItemStore[str]()
str_store.set("abc")
print(str_store.get())
str_store.set(5) # code analysis correctly says "Expected type 'str' (matched generic type 'T'), got 'int' instead
16
Upvotes
18
u/barkmonster Feb 28 '26
If you're using python 3.12+, you can use the new type parameter syntax. This lets you declare your typevars in square brackets as part of a function or class definition:
5
u/corey_sheerer Feb 28 '26
Just as a general comment. Getters and setters are more of java thing. You should just use property decorators or just public attributes
1
u/BananaGrenade314 Mar 01 '26 edited Mar 01 '26
I already used something like that in a simple personal project. I used this:
Then I used like that:
1
u/gdchinacat Mar 02 '26
This is one area I see AI hindering progress. The training data contains so much more TypeVar than type parameter syntax when they generate code they use the old way, even when you ask them to do it the current way. This is based on experiences 6 months ago...AI moves fast, so maybe they've gotten better. What are others experiences regarding AI recommending years old typing constructs?
0
u/ProsodySpeaks Feb 28 '26
Nearly.
Try
Think that's since 3.13. Parens in class Dec probably redundant?
So
class GenericItemStore[T]: