Skip to content

Use Literal.__new__ to select optimized subclasses #413

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

Merged
merged 4 commits into from
Jun 16, 2022

Conversation

djpohly
Copy link
Contributor

@djpohly djpohly commented Jun 16, 2022

This turns Literal() into a factory which creates an object of the appropriate type from the start, rather than having to overwrite the __class__ attribute later.

See #412 for the suggestion.

Question: should Empty be made a subclass of Literal? It seems to make sense that it would be.

This turns Literal() into a factory which creates an object of the
appropriate type from the start, rather than having to overwrite the
__class__ attribute later.
@codecov-commenter
Copy link

codecov-commenter commented Jun 16, 2022

Codecov Report

Merging #413 (0f1386f) into master (6dcf9aa) will increase coverage by 0.22%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #413      +/-   ##
==========================================
+ Coverage   90.87%   91.10%   +0.22%     
==========================================
  Files          11       11              
  Lines        3935     3946      +11     
  Branches      955      957       +2     
==========================================
+ Hits         3576     3595      +19     
+ Misses        213      206       -7     
+ Partials      146      145       -1     
Impacted Files Coverage Δ
pyparsing/core.py 90.64% <100.00%> (+0.36%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6dcf9aa...0f1386f. Read the comment docs.

djpohly added 2 commits June 16, 2022 15:31
Instance attributes from superclasses weren't being transferred to the
copy.  Regression test included.
This unifies the logic with other optimized literal classes like
_SingleCharLiteral, and it seemed right in terms of a type relationship.
@ptmcg
Copy link
Member

ptmcg commented Jun 16, 2022

Question: should Empty be made a subclass of Literal? It seems to make sense that it would be.

If Literal.__new__ can return an Empty, then that seems logical that Empty would be a subclass of Literal. Go ahead and include that change too (it should also help some of the internal type checking).

@ptmcg
Copy link
Member

ptmcg commented Jun 16, 2022

Also nice to get rid of the assignment to __class__, which always gets me some dirty looks.

@ptmcg ptmcg merged commit af4ba2c into pyparsing:master Jun 16, 2022
@djpohly djpohly deleted the empty-literal branch June 17, 2022 00:35
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.

3 participants