diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2022-11-22 16:07:49 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2022-11-22 16:07:49 +0000 |
| commit | cbc42334b15ad7cef03b3887be76f4bc2c0ff3ee (patch) | |
| tree | 76b84c1510b93fa121548b461ac308239a37db7f /lib/sqlalchemy/orm | |
| parent | 447249e8628ff849758c1a9cdf822ae060b7cb8b (diff) | |
| parent | 509ffeedefca1ad0ad8e29c6c3410d270fb3d2b9 (diff) | |
| download | sqlalchemy-cbc42334b15ad7cef03b3887be76f4bc2c0ff3ee.tar.gz | |
Merge "fix optionalized forms for dict[]" into main
Diffstat (limited to 'lib/sqlalchemy/orm')
| -rw-r--r-- | lib/sqlalchemy/orm/properties.py | 6 | ||||
| -rw-r--r-- | lib/sqlalchemy/orm/util.py | 23 |
2 files changed, 23 insertions, 6 deletions
diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 785a1a098..b8e1521a2 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -436,7 +436,7 @@ class ColumnProperty( try: return ce.info # type: ignore except AttributeError: - return self.prop.info + return self.prop.info # type: ignore def _memoized_attr_expressions(self) -> Sequence[NamedColumn[Any]]: """The full sequence of columns referenced by this @@ -684,10 +684,10 @@ class MappedColumn( ) -> None: sqltype = self.column.type - if is_fwd_ref(argument): + if is_fwd_ref(argument, check_generic=True): assert originating_module is not None argument = de_stringify_annotation( - cls, argument, originating_module + cls, argument, originating_module, include_generic=True ) if is_union(argument): diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index e5bdbaa4f..06f2d6d1d 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -2097,13 +2097,30 @@ def _cleanup_mapped_str_annotation( stack.append(g2) break - # stack: ['Mapped', 'List', 'Address'] - if not re.match(r"""^["'].*["']$""", stack[-1]): + # stacks we want to rewrite, that is, quote the last entry which + # we think is a relationship class name: + # + # ['Mapped', 'List', 'Address'] + # ['Mapped', 'A'] + # + # stacks we dont want to rewrite, which are generally MappedColumn + # use cases: + # + # ['Mapped', "'Optional[Dict[str, str]]'"] + # ['Mapped', 'dict[str, str] | None'] + + if ( + # avoid already quoted symbols such as + # ['Mapped', "'Optional[Dict[str, str]]'"] + not re.match(r"""^["'].*["']$""", stack[-1]) + # avoid further generics like Dict[] such as + # ['Mapped', 'dict[str, str] | None'] + and not re.match(r".*\[.*\]", stack[-1]) + ): stripchars = "\"' " stack[-1] = ", ".join( f'"{elem.strip(stripchars)}"' for elem in stack[-1].split(",") ) - # stack: ['Mapped', 'List', '"Address"'] annotation = "[".join(stack) + ("]" * (len(stack) - 1)) |
