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

Include the line number of the class declaration in binding keys #62

Merged
merged 9 commits into from
Nov 4, 2024

Conversation

bcmills
Copy link
Contributor

@bcmills bcmills commented Oct 15, 2024

Previously, we were using only the class name and filename as the binding key, which caused collisions if the same name was used to declare more than one (local) class in the same file (as in test_registry_attrs.py).

In the process, this simplifies the process of getting the stack frames: we had been retrieving the same frames (redundantly) in two different ways in the process of defining a field, but the potential sharing was somewhat obscured because the code was split into very short functions.

Fixes #43.

Previously, we were using only the class name and filename as the
binding key, which caused collisions if the same name was used to
declare more than one (local) class in the same file (as in
test_registry_attrs.py).

In the process, this simplifies the process of getting the stack frames:
we had been retrieving the same frames (redundantly) in two different
ways in the process of defining a field, but the potential sharing was
somewhat obscured because the code was split into very short functions.

Fixes #43.
@mmchenry-duolingo
Copy link
Contributor

I think we should just fix that test - it seems like a bug anyways to have two classes with the same name. I'm not sure we need to include the line number, but I suppose it doesn't hurt.

Also expand testing matrix to include more Python versions.
Python 3.8 has slightly different inspect.stack behavior from both 3.7
and 3.10, and given those differences we should probably be testing at
least one of each minor release version.
@bcmills
Copy link
Contributor Author

bcmills commented Oct 16, 2024

I think we should just fix that test - it seems like a bug anyways to have two classes with the same name.

Having two classes with the same name isn't a bug: each class is defined within the scope of a function, so they really are distinct classes (with different class IDs), and they don't collide.

This also applies to nested classes, in which the inner class might have the same reported function name as the outer class (TestClass vs. TestClass.TestClass). I've added a regression test to illustrate that case.

I'm not sure we need to include the line number, but I suppose it doesn't hurt.

If we could assume that every class with @inject_field annotations is correctly paired with an @inject_define decorator, and that inner classes always have names that are distinct from outer ones, then I think it would suffice to index by class name. But confirming that those assumptions hold (so that we can report violations) seems even more complex than avoiding them in the first place. 🤷‍♂️

@bcmills
Copy link
Contributor Author

bcmills commented Nov 4, 2024

Added a minor version bump and a make clean target to clear caches and venv.

@bcmills
Copy link
Contributor Author

bcmills commented Nov 4, 2024

Merging as v1.1.1.

@bcmills bcmills merged commit 46cb5d0 into master Nov 4, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

inject_define uses the wrong bindings if different classes with the same name are declared in the same file
3 participants