-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Variable Explorer error when class has attribute that references a generator #23378
Comments
I've continued to look at the code I wrote. This seems to happen when I have a method inside of another method. I pulled the inner method out and made it a staticmethod. As soon as I did that, the variable explorer was able to see the class again. I don't know if that is helpful for others or not, but I know this is an issue outside of Spyder. |
@puastro, thanks for taking a closer look at this problem. Could you post a simple code example that generates it? That way we'll be able to reproduce and try to fix it on our side. |
The following is a code that I was working on. It gives me the 'TypeError: cannot pickle 'generator' object' error. import numpy as np
class Geometry:
def __init__(self, L, r_max):
self.L = L # [m] Length of Nozzle
self.r_max = r_max # [m] Maximum radius of Nozzle
def generate_radius_profile(self, n_x):
self.r = np.array(self.nozzle_radius(x) for x in np.linspace(0, self.L, n_x))
# Define nozzle geometry function (e.g., a simple converging-diverging nozzle)
def nozzle_radius(self,x):
if x < 5.0:
return 0.5 + 0.1 * x
else:
return 1.0 + 0.05 * (x - 5.0)
def get_radius_at(self, x):
return self.nozzle_radius(x)
class Grid:
def __init__(self, L, r_max, n_x, n_r, geometry):
self.L = L
self.r_max = r_max
self.n_x = n_x
self.n_r = n_r
self.geometry = geometry
self.dx = L / (n_x - 1)
self.dr = r_max / (n_r - 1)
# Create grid arrays for x and r
self.x = np.linspace(0, L, n_x)
self.r = np.linspace(0, r_max, n_r)
# Generate the radius profile based on nozzle geometry
self.geometry.generate_radius_profile(n_x)
# Generate grid points using the nozzle geometry
self.X, self.R = np.meshgrid(self.x, self.geometry.r)
if __name__ == '__main__':
geom = Geometry(L=10.0, r_max=1.2)
grid = Grid(L=10.0, r_max=1.2, n_x=100, n_r=50, geometry=geom) |
The only way I found to solve this was to convert generator expression in the generate_radius_profile(self, n_x) to a list using the following line: self.r = np.array(list(self.nozzle_radius(x) for x in np.linspace(0, self.L, n_x))) after doing that, it allowed me to use the variable explorer as normal. |
Thanks for the reproducible example @puastro! I got a similar error on Linux too. After a bit of googling I found that generators can't be pickled, and that's the cause of this problem: https://stackoverflow.com/questions/7180212/why-cant-generators-be-pickled We'll report a better message about this limitation in a future version. |
Description
What steps will reproduce the problem?
I hit the 'reload' button in the corner of the variable explorer. It was open to the class I was looking at. It crashed as soon as I hit it.
Traceback
Versions
Dependencies
The text was updated successfully, but these errors were encountered: