238 questions
1
vote
1
answer
58
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 ...
0
votes
0
answers
27
views
How to fix isinstance failing in pytest when object comes from a different module [duplicate]
If one module returns an instance from another module, isinstance in pytest fails. My directory structure:
project/
|--pytest.ini
|--src/
|----__init__.py
|----bar.py
|----foo.py
|--test/
|----test_1....
1
vote
1
answer
66
views
How to check whether an sklearn estimator is a scaler?
I'm writing a function that needs to determine whether an object passed to it is an imputer (can check with isinstance(obj, _BaseImputer)), a scaler, or something else.
While all imputers have a ...
0
votes
1
answer
127
views
How do I check if something is a dataframe?
This seems like it should be simple.... but...
I am trying to write a function where I first want to make sure that what has been passed into it is actually a dataframe. So something like:
def ...
1
vote
2
answers
1k
views
Why does Python not allow Generics with isinstance checks?
Running the below code with Python 3.12.4:
from typing import Generic, TypeVar
T = TypeVar("T")
class Foo(Generic[T]):
def some_method(self) -> T:
pass
isinstance(Foo[int]()...
0
votes
1
answer
136
views
MyPy does not recognise x[idx] as boolean after isinstance(x[idx], bool)
Core issue
MyPy is not able to identify / validate the return value x[idx] as being of boolean type.
if isinstance(x[idx], bool) is True:
return x[idx]
Complete example code
from typing import ...
0
votes
0
answers
37
views
Create alias of class with different properties (e.g. docs) but passing all cross `isinstance` checks
I have a class and want to create an alias for it. However, I want all instances of the alias class to pass the instance check with the original and alias class, and the same for all instances of the ...
0
votes
0
answers
54
views
In python is it possible to test if an object is an instance of a subclass of MyClass but not an instance of MyClass? [duplicate]
I want to test if an object is an instance of a subclass but not an instance of the parent class. Example:
class MyClass():
pass
class Sub1(MyClass):
pass
class Sub2(MyClass):
pass
obj = Sub1(...
-1
votes
2
answers
168
views
Is there a way to replace an integer in a list, with a string from another list at random?
I have a list of words which are also assigned a numerical value
words = [[Happy,1],[Sad,2],[Anger,3]]
I then have two additional lists, of just words and integers, produced in earlier code
List1 = [...
1
vote
0
answers
55
views
Python: isinstance doesn't work properly?
I've got a problem with isinstance and I've reduced it to this case.
I have two files.
main.py:
import testType
class TestClass:
def __init__(self):
pass
if __name__ == '__main__':
...
0
votes
1
answer
225
views
Metaclass isinstance not working as expected
I've got the following class hiearchy:
class Acting: pass
class Observing: pass
class AgentMeta(type):
def __instancecheck__(self, instance: Any) -> bool:
return isinstance(instance, ...
0
votes
0
answers
33
views
"InfluxDBReader" and "StockPriceObserver" are not defined
@ModelFactory.register_model
class OverExtendedGapDown(BaseModel):
def __init__(self, data_provider, mode):
super().__init__(data_provider)
self.data_provider = data_provider
...
0
votes
0
answers
35
views
how can i put a message error if the variable is not an integer [duplicate]
im doing a calculator and im trying to give a message when the typed on the input is not an int rather than the program just closing(on the exe file),it gives an error when running in vscode.can ...
0
votes
0
answers
28
views
Amend the classes to include the following functionality
Amend your looking method to create a single string representing the animals and birds in the zoo. Hint: create a static method that accepts the concatenated list of animal list and bird list and uses ...
1
vote
3
answers
2k
views
isinstance with string representing types
isinstance("my string", "str | int")
isinstance("my string", "list")
Is there a way to check the type of a variable ("my string" in this case) based ...