Skip to content

Comments

Fix name mangling and typevar errors#895

Closed
junholee6a wants to merge 1 commit intocapitalone:devfrom
junholee6a:fix-name-mangle-error
Closed

Fix name mangling and typevar errors#895
junholee6a wants to merge 1 commit intocapitalone:devfrom
junholee6a:fix-name-mangle-error

Conversation

@junholee6a
Copy link
Contributor

@junholee6a junholee6a commented Jun 22, 2023

Issue: #722

Inside the BaseDataProcessor class definition, all references to __subclasses are automatically replaced with
_BaseDataProcessor__subclasses. This remains the case even in static methods _register_subclass() and get_class(). Same with BaseModel and its __subclasses field. So we do not have to write out the full name mangled identifiers inside the class definitions.

Also, mypy doesn't seem to be able to handle the return type of BaseDataProcessor.get_class() being a typevar, so that was changed to type[BaseDataProcessor]. This does not affect the functionality of get_class() since it always returns a subclass of BaseDataProcessor.

Inside the BaseDataProcessor class definition, references to
__subclasses are automatically replaced with
_BaseDataProcessor__subclasses. This remains the case even in static
methods _register_subclass() and get_class(). Same with BaseModel and
its __subclasses field. So we do not have to write out the full name
mangled identifiers inside the class definitions.

Also, mypy doesn't seem to be able to handle the return type of
BaseDataProcessor.get_class() being a typevar, so that was changed to
type[BaseDataProcessor]. This does not affect the functionality of
get_class() since it always returns a subclass of BaseDataProcessor.
@JGSweets
Copy link
Contributor

Issue: #722

Inside the BaseDataProcessor class definition, all references to __subclasses are automatically replaced with _BaseDataProcessor__subclasses. This remains the case even in static methods _register_subclass() and get_class(). Same with BaseModel and its __subclasses field. So we do not have to write out the full name mangled identifiers inside the class definitions.

Also, mypy doesn't seem to be able to handle the return type of BaseDataProcessor.get_class() being a typevar, so that was changed to type[BaseDataProcessor]. This does not affect the functionality of get_class() since it always returns a subclass of BaseDataProcessor.

IIRC, there's actually intent to use _BaseDataProcessor__subclasses if a different cls was used to call create might be an anti-pattern though.

@taylorfturner taylorfturner added the Question Further clarification requested label Jun 22, 2023
@junholee6a
Copy link
Contributor Author

IIRC, there's actually intent to use _BaseDataProcessor__subclasses if a different cls was used to call create might be an anti-pattern though.

@JGSweets Good point, I hadn't thought the case where cls is a subclass of BaseDataProcessor

Fortunately, it seems that since get_class() and _register_subclass() were defined in BaseDataProcessor, __subclasses is interpreted as _BaseDataProcessor__subclasses even if they are called with cls being a subclass of BaseDataProcessor. For example, this modified test still passes:

Screenshot 2023-06-22 at 12 39 46 PM

@JGSweets
Copy link
Contributor

IIRC, there's actually intent to use _BaseDataProcessor__subclasses if a different cls was used to call create might be an anti-pattern though.

@JGSweets Good point, I hadn't thought the case where cls is a subclass of BaseDataProcessor

Fortunately, it seems that since get_class() and _register_subclass() were defined in BaseDataProcessor, __subclasses is interpreted as _BaseDataProcessor__subclasses even if they are called with cls being a subclass of BaseDataProcessor. For example, this modified test still passes:

Screenshot 2023-06-22 at 12 39 46 PM

I wonder if that is a change in py version. Maybe 3.6 or 3.7 didn't allow this, but we no longer support it anyways.

def get_class(cls: type[Processor], class_name: str) -> type[Processor] | None:
def get_class(
cls: type[BaseDataProcessor], class_name: str
) -> type[BaseDataProcessor] | None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, makes sense that it isn't type[Processor]. While generics make sense at the top of the class, this output isn't related to the cls, but to the str.

@JGSweets
Copy link
Contributor

@taylorfturner do we update for this to go into dev?

@taylorfturner
Copy link
Contributor

@taylorfturner do we update for this to go into dev?

Definitely

@taylorfturner taylorfturner changed the base branch from main to dev June 23, 2023 19:28
@taylorfturner
Copy link
Contributor

taylorfturner commented Jun 27, 2023

Because we are changing the base to dev here, you may need to re-do this depending on where you branched from -- ideally you branch from dev for this and then merge back to dev... or you can cherrypick commits from here onto a new branch off of dev @junholee6a -- thanks for all your diligent work here. We've been busy with a lot of PRs lately, but we haven't forgot about these!

@junholee6a
Copy link
Contributor Author

Because we are changing the base to dev here, you may need to re-do this depending on where you branched from -- ideally you branch from dev for this and then merge back to dev... or you can cherrypick commits from here onto a new branch off of dev @junholee6a -- thanks for all your diligent work here. We've been busy with a lot of PRs lately, but we haven't forgot about these!

I'm re-making my PRs based on dev, so I'll close this one. This is a fulfilling project to work on due to all the feedback I get, and I'm not in a rush to have things merged so don't worry! Thanks :)

@junholee6a junholee6a closed this Jun 28, 2023
auto-merge was automatically disabled June 28, 2023 21:55

Pull request was closed

@junholee6a junholee6a deleted the fix-name-mangle-error branch July 23, 2023 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Question Further clarification requested static_typing mypy static typing issues Work In Progress Solution is being developed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants