Repository navigation
feat: opt-in blob size limit and static-first foreign-key lookup - #1738
Open
dimitri-yatsenko wants to merge 6 commits into
Open
dimitri-yatsenko wants to merge 6 commits into
dimitri-yatsenko wants to merge 6 commits into
Conversation
dimitri-yatsenko
requested review from
MilagrosMarin,
lum-agilyti and
ttngu207
November 21, 2026 02:06
…me check in describe() - blob.max_decompressed_size (DJ_BLOB_MAX_DECOMPRESSED_SIZE): refuse a compressed blob whose recorded uncompressed size exceeds it, before decompressing. Unset by default, so nothing changes unless it is set. - resolve_foreign_key_reference looks each .name step up with inspect.getattr_static. - describe() checks each projected foreign-key attribute name it reads from the database. Refs #1717.
resolve_foreign_key_reference used inspect.getattr_static alone, so a reference through a module __getattr__ (a module that loads its tables lazily), a class __getattr__ or a property no longer resolved. The static lookup still runs first and resolves module attributes, table classes and part tables without running code. getattr runs only when the static lookup finds no attribute, or finds a descriptor such as a property.
…ot read back Python keywords such as `class` are valid attribute names, and parse_foreign_key_reference reads them back as .proj() keyword arguments. The identifier check refused them, so describe() failed for tables it described before. The check now writes the new name into a .proj() call and parses it back: a name read back as itself is written as before; one that is not (`x=1`, quotes, backslashes) raises. Add a test that tells the static-first lookup apart from getattr alone: an object whose __getattribute__ returns another table, or raises, for a name that is a table class attribute.
…rning; diagrams render such tables The foreign-key reference parser took a name only from \w characters, which miss characters that Python identifiers allow, such as combining marks and U+00B7. DataJoint 2.3.5 and earlier declared such names. The parser now takes every Python identifier as a name, so describe() writes them in a form that alter() reads back. For a projected foreign-key attribute whose new name is not an identifier (a column named by SQL, such as x=1 or a-b), no form of the reference holds the name. describe() no longer raises for it: it writes the name as is, as master does, and emits a warning. alter() raises for such a table instead of parsing that text. dj.Diagram gives a table whose describe() raises DataJointError a tooltip with only its name, and escapes double quotes in the attr_map edge attribute, which made the DOT source invalid for a column named a"b or a'b. One table no longer stops the diagram of its schema.
pydot quotes a tooltip and escapes its double quotes, but leaves its backslashes as they are. A referenced column named p\ by SQL escaped the closing quote of the renamed foreign key's edge tooltip, and the \" that describe() writes for a referenced column named p"id closed the child's node tooltip early: Graphviz rejected the DOT source. Both tooltips now double their backslashes, which Graphviz shows as one. The warning describe() emits for a projected name that alter() cannot read back is ignored while the diagram writes a tooltip, so it does not affect diagrams, also under -W error.
dimitri-yatsenko
force-pushed
the
fix/definition-blob-follow-ups
branch
from
November 21, 2026 08:07
62968b5 to
259b1b8
Compare
The diagram tests raised every warning as an error. With the minimum networkx version, to_pydot emits a DeprecationWarning of its own, which failed them. They now raise only the warning describe() emits for a name that alter() cannot read back, which the diagram must not let through.
This branch has not been deployed
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
Follow-ups to the 2.3.6 changes for table definitions read back from the database and for compressed blobs (GHSA-gmj8-m8g8-9wg9).
blob.max_decompressed_size(environment variableDJ_BLOB_MAX_DECOMPRESSED_SIZE) refuses a compressed blob whose recorded uncompressed size exceeds it. The check runs before anything is decompressed. It is unset by default, so nothing changes unless a deployment sets it.resolve_foreign_key_referencefollowed each.namestep withgetattr, which runs a property, or a custom__getattr__, of the object it walks. Each step now first usesinspect.getattr_static, which only looks the name up: module attributes, table classes and part tables resolve this way without running code. The dynamicgetattris the fallback, and it runs only when the static lookup finds no value: when the object has no such attribute (a module-level or class__getattr__may supply it, as in a module that loads its tables lazily), or when the attribute is a descriptor such as a property. References through these resolve as before.\w, which misses characters Python allows in identifiers: combining marks (the vowel sign in the Hindiकिताब),·(U+00B7) and a few others. DataJoint 2.3.5 and earlier declared such names, so a table with a projected foreign key named this way could not be altered on 2.3.6. The parser now reads every Python identifier as a name, and still refuses anything else.describe()writes-> Parent.proj(new="old")from the foreign-key attribute map read from the database. The referenced name is quoted, but the new name is written as is, since a keyword argument name cannot be quoted, andalter()parses that text back. A column created or renamed by SQL can have a name that is not an identifier (for examplex=1,a-b,1st_ref,a$b, or a name with a quote or a backslash); no form of the reference holds such a name, and the textdescribe()writes for it may parse as a different foreign key. For such a name,describe()writes it as before and emits a warning, andalter()raises aDataJointErrorinstead of parsing that text.dj.Diagramrenders a schema that holds such a table. A diagram writes each table'sdescribe()into its tooltip, so one table could stop the diagram of the whole schema. A table whosedescribe()raises aDataJointErrornow gets a tooltip with only its name. The warningdescribe()emits for a name thatalter()cannot read back is ignored while the diagram writes a tooltip, so it does not affect diagrams, also under-W error. Two characters made the DOT source invalid, and are now escaped:a"bora'b.p\escaped the closing quote of the edge tooltip, and for a referenced column namedp"id, the\"thatdescribe()writes closed the node tooltip early. Backslashes are now doubled, and Graphviz shows each pair as one.Refs #1717.
Behavior change
blob.max_decompressed_sizeset, fetching a compressed blob whose recorded uncompressed size exceeds it raisesDataJointError. Unset (the default), nothing changes.describe()emits aUserWarningand writes the name as before, andalter()raisesDataJointError. Names that are identifiers, Python keywords included, are written and read back as before.dj.Diagramrenders a schema in which a table'sdescribe()raisesDataJointError; that table's tooltip shows only its name.dj.Diagramdoes not emit the warning ofdescribe(), and renders a schema whose column names hold double quotes or backslashes. Its tooltips show those characters as written.__getattr__and through properties. Code of the object walked runs only for those cases, no longer for names the static lookup finds.Changes
src/datajoint/settings.py: ablobsettings group withmax_decompressed_size(None or a non-negative integer).src/datajoint/blob.py:unpack()andBlobtakemax_decompressed_sizeand check the recorded size before decompressing. Uncompressed blobs are not affected: they are already in memory.src/datajoint/builtin_codecs/blob.py:BlobCodec.decodetakescontextand passes the limit from the connection's config.src/datajoint/declare.py:_lookup_attribute()for.namesteps:inspect.getattr_staticfirst,getattronly when that finds no attribute or finds a descriptor.str.isidentifier()decides whether it is a name.src/datajoint/table.py:_is_projection_name()writes the new name into a.proj()call and parses it back withparse_foreign_key_reference, the parseralter()uses; the name must come back unchanged.describe()moves to_describe(context, strict), which collects the new names that are not read back.describe()warns for them;alter()calls_describe(..., strict=True), which raises.src/datajoint/diagram.py:describe()raisesDataJointError, anddescribe()runs with warnings ignored._encapsulate_edge_attributesescapes double quotes.Tests
tests/unit/test_blob_decompression.py):BlobCodecapplies the limit from the config it is given.use_32bit_dims), go through the limit and through the existing size-mismatch checks.tests/unit/test_foreign_key_reference.py):__getattr__, on an object and on a module that both define one.__getattribute__returns another table, or raises, the reference resolves to the class attribute and__getattribute__does not run.getattralone gives the other table, or the error, so this test fails withgetattralone.__getattr__), a class__getattr__and a property still resolve, and each runs exactly once. These three fail with static lookup alone.could not be resolved: no attribute ....parent_ref, the keywordsclass,fromandlambda,_private,__x,Größe, andकिताब_id,x·y,℘x,x‿y, whose characters\wdoes not match) are accepted, with a referenced name holding quotes and a backslash. Names that are not identifiers (x=1, names with",',\, a space, a no-break space,#,),.,-,$, a full-width=or a line break, a leading ASCII or Arabic-Indic digit,a="x",b, the empty name) are refused.किताब.Partandx·y.Partresolve.tests/integration/test_definition_blob_follow_ups.py), on MySQL and PostgreSQL:<blob>with the limit set below, and above, its size.#and', and a projected foreign key, throughdescribe()andalter(). The test adds an attribute with such a comment, and a secondalter()finds nothing to change.class,from,किताब_idorx·yis declared,describe()writes-> Parent.proj(<name>="parent_id"),alter()reads it back with nothing to change, anddj.Diagram(schema).make_svg()renders.x=1,a"b,a'b,a\b,a\"b,a-b,a b,1st_refora$b:describe()warns, naming it, and writes it as is with the rest of the definition;alter()raises and names it.dj.VirtualModule, renders withmake_svg()with the warning ofdescribe()raised as an error.parent_idis renamed in the database top"idorp\(ending in a backslash), the diagram renders with that warning raised as an error, and its edge tooltip showsparent_ref ← p"idorparent_ref ← p\.describe()raisesDataJointErrorgets its name as its tooltip; the other table keeps its definition, andmake_svg()renders.test_static_lookup_comes_before_getattrguard behaviorgetattralone also had. The keyword tests pass onmaster, which writes every name as is; they guard against refusing names that work.test_diagram_of_schema_with_projected_name_not_read_backpasses onmasterfor names without a double quote (x=1,a\b,a-b,a b,1st_ref,a$b), becausemasteremits no warning; those cases check that the new warning does not reach the diagram. Thea"b,a'banda\"bcases andtest_diagram_with_referenced_column_renamedfail onmaster. The full suite passes locally on MySQL and PostgreSQL.Documentation
datajoint/datajoint-docs#317 documents
blob.max_decompressed_size(DJ_BLOB_MAX_DECOMPRESSED_SIZE) and merges after this PR.Not in this PR
The issue asks to resolve foreign-key references without running properties. This PR looks names up statically first, but keeps
getattras the fallback, so a property, or a module or class__getattr__, on the path of a reference still runs: dropping the fallback would break references through modules that load their tables lazily. Whether to drop it stays open in #1717, so this PR refers to the issue instead of closing it.Notes for reviewers
"blob.max_decompressed_size": ("DJ_BLOB_MAX_DECOMPRESSED_SIZE", ...)to that list.