1,222 questions
0
votes
1
answer
73
views
Python in vscode does not autocomplete or suggest built in class method
I am using vscode for Python dev. I have the Microsoft Extensions (Pylance, Python, Python Debugger, PythonEnviroments) installed and working.
The issue is when I try to make a metaclass in vscode ...
0
votes
1
answer
58
views
Understanding Metaclass Interaction with __init_subclass__ and Class Decorators
I'm trying to wrap my head around the precise order of operations and interactions when a Python class definition involves a metaclass, a init_subclass method, and a class decorator.
class Meta(type):
...
1
vote
1
answer
59
views
Python @classmethod and @staticmethod decorators cause isinstance(…, FunctionType) to be False
I am writing a Python metaclass that creates a special index of class members that meet certain criteria, one criterion being that those members should not be functions. I am checking this criterion ...
1
vote
1
answer
76
views
Exception thrown in python magic method is lost on the way
For a project, I want to prevent the use of certain methods in an overriding class.
As this happens rather frequently, I am using a metaclass to block many methods at once (how and why is not within ...
0
votes
1
answer
159
views
KeyError: 'self' in save_hyperparameters() when custom metaclass used - Pytorch Lightning
Description
I'm working with LightningDataModule and wanted to ensure that a method (_after_init) runs only once after full initialization, regardless of subclassing. For that, I implemented a custom ...
0
votes
3
answers
118
views
How can I use metatypes to show a View in SwiftUI?
I have a SwiftUI View that I would like to be able to show one of many different other views:
struct View1: View {
var body: some View {
Text("View1")
}
}
struct View2: View ...
0
votes
1
answer
99
views
How to replace base class in python [closed]
Here is the code:
class A: pass
class B: pass
class Example(A, B): pass
...a lot of method
# new class B called BPlus
class BPlus: pass
# want a new class Example that base on (A, BPlus) but ...
0
votes
1
answer
184
views
Subclassing typing.TypeDict
I skimmed over PEP-0589, and I am wondering why typing.TypedDict works when subclassing it like this:
class A(TypedDict, total=False):
x: int
y: int
Specifically, TypedDict is a function that ...
0
votes
2
answers
67
views
Executing code on class (metaclass instance) destruction in python
We have some API that should be shut down (e.g. api.shutdown()) just once per python process and specific only to a particular class (e.g. ControllerA) from a hierarchy of controllers (e.g. Controller ...
0
votes
1
answer
41
views
Python metaclass keyword arguments not getting used by subclass
I am trying to write a metaclass to assist in serialization. The intention of the metaclass was to isolate the production code from the serialization mode (e.g. YAML or JSON) as much as possible. So, ...
3
votes
1
answer
358
views
Creating a metaclass that inherits from ABCMeta and QObject
I am building an app in PySide6 that will involve dynamic loading of plugins. To facilitate this, I am using ABCMeta to define a custom metaclass for the plugin interface, and I would like this custom ...
1
vote
1
answer
78
views
Can't delete attribute added by metaclass
I've been playing around with Python metaclasses, and I have encountered a rather strange instance where I cannot delete an attribute added to a class object by a metaclass. Consider the following two ...
0
votes
1
answer
45
views
Python Object and Metaclass [closed]
i am new in python and having some confusions. As Java, in python also it is showing that Object is the top-most class but then what is the use of Metaclass as it is showing that all classes are ...
0
votes
1
answer
90
views
`TypeError: metaclass conflict` for a Subclass of `type`
I made an abstract base class MyABC, whose metaclass is, of coarse, abc.ABCMeta. Then, I wanted to make a class SubType, whose instances are a subclass of MyABC. Since the instance of it is a class, I ...
3
votes
1
answer
29
views
What are the allowed values / source spec for umlRole in a metaconstraint?
In a UML definition of a metamodel it is possible to define metaconstraints e.g.
<<metaconstraint>> umlRole="classifier"
<<metaconstraint>> umlRole = "class"...
1
vote
1
answer
82
views
How can I get IDEs to recognize a desired type name for a statically declared, dynamically created class in Python?
Problem:
I am working on a library with, e.g., support for a UInt5 type, an Int33 type, etc. The library is a little more complicated than this, but for the sake of example creating a UInt12 type ...
0
votes
1
answer
83
views
How can I refer to this metaclass inside a metaclass without specifying its name in the code?
I have some python code:
class Meta(type):
def __new__(cls, name, base, attrs):
attrs.update({'name': '', 'email': ''})
return super().__new__(cls, name, base, attrs)
def ...
1
vote
1
answer
76
views
Can I avoid the ```metaclass=``` when I define an inherited class?
I would like to have a custom metaclass that looks like the definition of a TypedDict.
When one declares a new TypedDict we write something like this:
from typing import TypedDict
class MyClass(...
2
votes
1
answer
336
views
Modify Enum to return value by default
I am trying to modify Enum to return value by default:
class EnumDirectValueMeta(EnumMeta):
def __getattribute__(cls, name):
try:
return object.__getattribute__(cls, name)....
1
vote
2
answers
122
views
How to prevent attribute re-access until all attributes have been accessed once (Python metaclass)?
I have a list of attributes, say ["foo", "bar", "baz"], and I want to write a metaclass which ensures the following properties:
They can all be accessed once, in any ...
0
votes
2
answers
67
views
How can I retain a reference to the parent object when creating an instance of a nested class through the outer object in Python?
Is it possible to retain a reference to the parent object when creating an instance of a nested class through the outer object, without explicitly passing the parent object as an argument?
class ...
1
vote
1
answer
255
views
Python - extend enum fields during creation
Is it possible extend enum during creation?
Example:
class MyEnum(enum.StrEnum):
ID = "id"
NAME = "NAME
And I need that after creating this enum contains the next fields:
ID = &...
0
votes
1
answer
183
views
Python use metaclass, cause Vscode can't give type hint
I create a metaclass and use it in class "Pool", then Vscode will not provide any type hintabout Pool
In python i create a metaclass like this:
class SignSingleton(type):
def __init__(...
0
votes
1
answer
25
views
Override a keyvalue that is passed to a metaclass of a Baseclass by its childClass
When I derive the BaseClass by the ChildClass, the BaseMetaClass.__new__(...) is run twice. First with the keywordarg "foo" and second time with "bar".
Is it possible to override &...
0
votes
0
answers
76
views
Are metaclasses objects? [duplicate]
I always hear or read that 'everything in Python is an object'. This sentence is really helpful for beginner programmers as myself, and I was quickly able to understand that classes, methods are ...