Comments (10)
I think if they do make a ClassVar
without using it they'll still get what they'd expect. A pure Python implementation of this would look like:
class _slot_with_default:
def __init__(self, default, orig_slot):
self.default = default
self.orig_slot = orig_slot
def __get__(self, inst, ctx):
if inst is None:
return self.default
try:
return self.orig_slot.__get__(inst, ctx)
except AttributeError:
return self.default
def __set__(self, inst, value):
self.orig_slot.__set__(inst, value)
class C:
__slots__ = ('x',)
C.x = _slot_with_default(42, C.x)
And so C.x
would return the default, C().x
would return the default, and you could still do some_c.x = 100
and get that value back out.
from cinder.
This could be a problem if people are used to thinking of class vars and object vars as living in two different namespaces
Python developers won't expect this, in Python they have always shared the same namespace.
from cinder.
We now report an error "Class attribute requires ClassVar[...] annotation: x" but support for the default is under way!
from cinder.
This looks like it should be allowed and seems like a valid bug, it looks like when we added final support our support for non-final class level declarations was broken. We're doing some tracking on whether or not the final attributes are assigned which might be too clever. If the attribute is not final, and assigned, then this should just represent a class level attribute.
from cinder.
from cinder.
To clarify for anyone not familiar with Python typing specs, I was using "PEP 484" as shorthand for the set of PEPs that specify Python typing; the actual PEP that introduced variable/attribute annotations and ClassVar is PEP 526: https://www.python.org/dev/peps/pep-0526/
from cinder.
Thinking about it more we could actually support the "instance variable with default" pattern as well. We'd just need to transform it into something like:
class C:
x: int = 3
slots = ('x', )
slot_defaults = ('x': x)
# or
slots_with_defaults = ('x', )
Such that we suppress the "ValueError: 'x' in slots conflicts with class variable" error and when the instance doesn't define it you get the default value.
With this specific "x: Final[int] = 3" case though, it seems like that's implicitly a ClassVar if it's assigned and an instance var without one per PEP 591? https://www.python.org/dev/peps/pep-0591/
from cinder.
Thinking about it more we could actually support the "instance variable with default" pattern as well.
We could, but I wonder if this would mostly lead to people trying to make class attributes without using ClassVar
and it seeming to work but then accessing the attribute on the class won't give them what they expect. It might be better to give a clear error outlining the options instead of guessing intent on x: int = 3
?
With this specific "x: Final[int] = 3" case though, it seems like that's implicitly a ClassVar if it's assigned and an instance var without one per PEP 591? https://www.python.org/dev/peps/pep-0591/
Ah good call, I didn't realize Final was special in that way. It seems a little unfortunate to me to give up the consistency of "class attrs always use ClassVar" in this specific case, but I understand the reasoning of the PEP: if it's Final
you can't reassign it so if it's initialized on the class and final, it must be a class attribute.
from cinder.
One downside of _slot_with_default
is that it puts a type constraint on instances. If a class declares x:int = 3
then its instances can't declare x:str = "X"
because they inherit a typed slot.
This could be a problem if people are used to thinking of class vars and object vars as living in two different namespaces. E.g. "My instance can use any field name. I don't need to avoid names that happen to be class vars."
from cinder.
This is now supported; we create a slot with a default value at the class level.
from cinder.
Related Issues (20)
- Type errors in deltablue_static HOT 3
- Build error: C++20 ambiguous & forming offset 4 is out of the bounds HOT 1
- fannkuch benchmark & implicit returns
- type aliases no longer work? HOT 2
- Error compiling with `--with-lto` -- undefined reference to `vtable for strictmod::objects::StrictExceptionObject`
- Cannot install insightface by python-cinder HOT 2
- RuntimeError: Failed to initialize CinderX HOT 3
- could not find symtab HOT 4
- Outdated instructions in python-build-env? HOT 5
- "Ghost" arguments are being passed to method calls! HOT 6
- add README for benchmarks HOT 7
- questions about CheckedDict and unknown types HOT 5
- Benchmark Cinder vs. Cpython performance; improve pipeline and performance of the Cinder build HOT 15
- 15 failing tests fail when running `make test` on a Mac HOT 1
- build error: listnode.gcda missing-profile HOT 3
- Cinder vs. Python 3.11 HOT 9
- can't compare double to Literal HOT 1
- nonsensical TypeError from "-X jit -m compile --static" HOT 3
- No pattern found for opcode Fadd: Xxr HOT 2
- HPy support HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cinder.