Skip to content
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

Delete the LandMine class #6

Closed
wants to merge 6 commits into from
Closed

Delete the LandMine class #6

wants to merge 6 commits into from

Conversation

AlexWaygood
Copy link

@AlexWaygood AlexWaygood commented Jul 31, 2023

@erlend-aasland, what do you think about this? It simplifies some of the typing, since now CConverter.function is always an instance of Function, instead of sometimes being an instance of LandMine. I'm not sure why we need to temporarily set self.function to be an instance of LandMine -- I feel like we can get the same effect by just not setting self.function until after converter_init() has been called. But I'm a bit wary of Chesterton's Fence here -- I'm not sure I understand fully exactly which problem the self.function = LandMine() thing was originally supposed to solve.

@erlend-aasland
Copy link

I don't think this is correct. The LandMine class is needed for custom converters. We don't have a lot of coverage for those; we should fix that. I was able to trigger the LandMine class last year when experimenting with custom converters for sqlite3, IIRC.

I'm sure we can fix it with a special case of Function, though; it does not have to be a separate class. And I think we should rename it to something nicer; land mines are not nice.

@AlexWaygood
Copy link
Author

AlexWaygood commented Jul 31, 2023

Hmmm, I'm still not quite sure I fully understand the situation where setting self.function to be a LandMine provides more safety than just not having self.function be set at all yet. But I'll trust you that it's needed!

I'm sure we can fix it with a special case of Function, though

Yes, maybe it can be a subclass of Function? DummyFunction, on which any attribute access raises AttributeError? That would also mean we could get rid of the assertions we added for mypy.

land mines are not nice

indeed

@erlend-aasland
Copy link

I have a test ready. Let's add that first :) You may be right; it could be sufficient to set self.function = None.

@erlend-aasland
Copy link

See python#107496

@AlexWaygood
Copy link
Author

AlexWaygood commented Jul 31, 2023

it could be sufficient to set self.function = None.

That wouldn't improve anything from a typing perspective: self.function would just change from having a Function | LandMine type to having a Function | None type. We'd still need the assert isinstance(self.function, Function) assertions.

What I'm suggesting is just not having self.function be set at all until after converter_init() has run, so that accessing self.function just raises AttributeError if you try to do it inside converter_init().

We could possibly add a custom __getattr__ method to the CConverter class so that it gives a nice error message when the AttributeError is raised inside converter_init() and the attribute is "function"

@erlend-aasland
Copy link

Aha, I misunderstood you. Yes, I like that.

@AlexWaygood
Copy link
Author

Thoughts on the updated version?

Tools/clinic/clinic.py Outdated Show resolved Hide resolved
@erlend-aasland
Copy link

Thoughts on the updated version?

It's nice. I think it would be helpful to include the attr name in the error message, since that can help debugging.

@AlexWaygood
Copy link
Author

AlexWaygood commented Jul 31, 2023

It's nice. I think it would be helpful to include the attr name in the error message, since that can help debugging.

Hmm. Now the attribute error is raised trying to access self.function itself rather than attributes of self.function, so that's somewhat harder. But maybe we could use fail() inside the __getattr__ method (instead of just raising AttributeError) to indicate the precise line number in the clinic input that triggered the error?

@erlend-aasland
Copy link

Hmm. Now the attribute error is raised trying to access self.function itself rather than attributes of self.function, so that's somewhat harder. But maybe we could use fail() inside the __getattr__ method (instead of just raising AttributeError) to indicate the precise line number in the clinic input that triggered the error?

Hm, true. Calling fail() inside __getattr__ is a possibility.

(BTW; the line numbers in the tests messed up because of FakeClinic; that's why most of the tests report the line number as 0.)

@erlend-aasland
Copy link

Let's take this to the cpython repo :)

@erlend-aasland
Copy link

By the way, one of the comments mention that the LandMine class is there to protect against certain types of cloning. We should try and add a test for that case as well; it may take a slightly different path, so we need to make sure we catch that (potentially) slightly different scenario.

@erlend-aasland
Copy link

By the way, one of the comments mention that the LandMine class is there to protect against certain types of cloning. We should try and add a test for that case as well; it may take a slightly different path, so we need to make sure we catch that (potentially) slightly different scenario.

Re-reading that comment, it kinda looks like we're covered. BTW, here's the commit that introduced it: 7726ac9

@AlexWaygood
Copy link
Author

here's the commit that introduced it: 7726ac9

whelp, there's a lot going on there

@erlend-aasland
Copy link

whelp, there's a lot going on there

IIRC, they used to queue approved commits, and them squash them all with a giant change log entry and/or git commit message. There's a lot of commits like that from the way old development workflow :)

@AlexWaygood
Copy link
Author

I filed python#107541

@AlexWaygood AlexWaygood closed this Aug 1, 2023
@erlend-aasland erlend-aasland deleted the delete-LandMine branch August 1, 2023 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants