Repository navigation
fix(spec): one number rule for the spec reader and the Python API - #570
Merged
Merged
Conversation
The numeric readers tested isinstance(value, (int, float)) exactly, so numpy.int64 from a pandas column was refused after the Python API had drawn a correct sheet from it. They now accept any numbers. Real, and whole-number fields any numbers.Integral, still excluding bool and still returning values unchanged. Decimal is accepted for line-number components and stream properties, which are only displayed, and remains refused for geometry, which cannot be computed with it. Refs: #536
A Decimal size, pin coordinate, via() waypoint or annotation layout field was accepted and crashed the render. They are now refused when set, through shared checks the spec reader also uses, and pin() ranks must be whole numbers. Stream color and dasharray must be text, since a bool or number broke the SVG or draw.io output. A numeric flag reference, table-sheet drawing number or subtitle is drawn as text, as title-block fields are. Also restore the docs/api.md note on integer number starts left out of the #533 commit. Refs: #536
Before Python 3.12 a Fraction cannot be formatted with ":g", and the renderers write every coordinate that way, so a Fraction size or position crashed the render on 3.10 and 3.11. The real-number rule now takes int, float and non-rational reals such as numpy floats, and refuses Fraction alongside Decimal on every Python version. Refs: #536
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Makes the spec reader and the Python API apply one rule to numbers. The reader accepted only exact
int/float, sonumpy.int64from pandas was refused after the Python API had drawn a correct sheet from it. Meanwhile the Python API accepted values that crashed the render:Decimalgeometry, non-text stream styles and numeric labels. Numpy numbers now round-trip; values layout cannot use are refused when set; numeric labels draw as text. No drawing changes.Related Issues
Closes #536
Refs: #533, #534
Changes
pandid/_checks.py, withis_real(anynumbers.Realexceptbool;Decimalis not one),is_whole(anynumbers.Integralexceptbool), andcheck_real/check_wholeraisingTypeErrorwith afloat(value)hint forDecimal.pandid/spec.py):120and120.0draw differently).Decimalis accepted where a value is only displayed (line-number components, stream properties) and refused for geometry.Unitwidth/height, at construction and on assignment,Blockincluded;pin()coordinates; ranks must be whole numbers, socol=1.0androw=Trueare refused;Stream.via()waypoints;Annotation/TableBoxmargin,width,font_sizeandposition, at construction and on assignment;Stream.coloranddasharraymust be text.referenceand the table sheet'ssheet_drawing_numberandsheet_subtitle, throughdocument._drawn_textas title-block fields are.tests/test_stated_values.py, 54 tests in total for to_dict() silently drops a stated value and the sheet is redrawn differently: a fractional number-start, and a falsey annotation title #533 and The spec reader refuses numpy and Decimal numbers the Python door accepts and draws #536.docs/api.mdstates the number rules, restoring the to_dict() silently drops a stated value and the sheet is redrawn differently: a fractional number-start, and a falsey annotation title #533 sentence on integer number starts that was missing from that commit.CHANGELOG.mdhas[Unreleased]entries.How to Test
Evidence
Every numeric field the spec reads was tried with
numpy.int64,numpy.float64andDecimal, through the Python API,to_svg,to_drawio,to_dictandfrom_dict.int64refused on most fieldsto_dict/from_dictDecimalin geometryTypeErrormid-renderTypeErrorwhen set; refused by the readerDecimalin components/propertiescolor=True,dasharray=4TypeErrorwhen setreference,sheet_drawing_number,sheet_subtitlemainfor 46 drawings (the 21 examples as authored and with placement stripped, plus four benchmark sheets).git diff --checkare clean. Pyright reports 0 errors plus the existing__all__warning, and 0 errors on the test file.Not changed
Decimalcounts (trays,feed_stages,n_inlets) still fail at construction with Python's ownTypeError.Decimalstream-table widths and font size draw through the Python API and are still refused by the reader.to_dict()are not converted, sojson.dumpsof such a spec fails.Checklist
mainand the branch is mergeable🤖 Generated with Claude Code