Skip to content

Conversation

@andfoy
Copy link
Contributor

@andfoy andfoy commented Apr 4, 2025

Fixes #35

This PR adds two new mechanisms to register custom functions whose invocation should cause a test to be executed in a single thread.

The first works by setting the variable __thread_unsafe__ to True, while the second one relies on static values provided via the INI configuration file. On both cases, the functions will be checked up to two levels in the call-stack (derived from the AST).

cc @rgommers

Copy link
Collaborator

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, mostly language suggestions and one small API suggestion. The test makes it clear that this is working as expected.

I didn't try messing with the SciPy tests and didn't verify if this fixes the issue Ralf originally had, but it looks like it should.

if len(node.targets) == 1:
name_node = node.targets[0]
value_node = node.value
if getattr(name_node, "id", None) == "__thread_unsafe__":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if getattr(name_node, "id", None) == "__thread_unsafe__":
if hasattr(name_node, "id", None) == "__thread_unsafe__":

That way the default is the same as if the attribute isn't defined at all and it doesn't matter what the value is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think hasattr does not allow for 3 arguments, whereas getattr does.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, sorry. Not literally that diff!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless you refer to marking the test as thread-unsafe by just defining __thread_unsafe__

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that’s exactly right. I’m saying you should only need to define the attribute and it can have any value.

If you want to keep the boolean flag that’s fine, but I think it should be spelled __thread_safe__ = False so the default is True. People have an easier time thinking about boolean flags that default to True.

Copy link
Collaborator

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one spot you missed, otherwise looks good.

I'm going to look at using this to fix #20 -- I happened to run pytest-run-parallel on a project that uses hypothesis and happen to need it!

README.rst Outdated
module.submodule2.func2
...
Also, if you define a `__thread_unsafe__` attribute on a function that is
Copy link
Collaborator

@ngoldbaum ngoldbaum Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Also, if you define a `__thread_unsafe__` attribute on a function that is
Also, if you set `__thread_safe__ = False` as an attribute on a function that is

@andfoy andfoy force-pushed the extend_skip_functions branch from 58bf49a to 85d281d Compare April 8, 2025 17:01
@ngoldbaum
Copy link
Collaborator

Thanks, let's do followups if for some reason SciPy needs additional changes.

@ngoldbaum ngoldbaum merged commit 8ffea33 into main Apr 9, 2025
9 checks passed
@rgommers rgommers deleted the extend_skip_functions branch April 19, 2025 21:07
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.

Need a method to mark arbitrary functions as thread-unsafe

3 participants