Skip to content
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

TypeError in ORM model using an enum type hint (1.4) #10065

Closed
1 task done
hofrob opened this issue Jul 5, 2023 · 7 comments
Closed
1 task done

TypeError in ORM model using an enum type hint (1.4) #10065

hofrob opened this issue Jul 5, 2023 · 7 comments
Labels
SQLA mypy plugin mypy plugin issues only. general pep-484 issues should be "typing" typing pep -484 typing issues. independent of "mypy"
Milestone

Comments

@hofrob
Copy link

hofrob commented Jul 5, 2023

Ensure stubs packages are not installed

  • [] No sqlalchemy stub packages are installed, this is against 1.4.x

Verify if the api is typed

  • The api is not in a module listed in #6810 so it should pass type checking

Describe the typing issue

"INTERNAL ERROR" when running mypy on a file with basic ORM models.

To Reproduce

import enum

import sqlalchemy
from sqlalchemy import orm

Base = orm.declarative_base()


class BlaEnum(enum.Enum):
    SOMETHING = enum.auto()


class Model(Base):
    ...


class Bar(Model):
    size: BlaEnum = sqlalchemy.Column(sqlalchemy.Text)

Error

version: 1.5.0+dev.c13f1d416e907f58bc77d086b84819f500f1bde9
Traceback (most recent call last):
  File "/path/to/project/venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/main.py", line 94, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/main.py", line 173, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/build.py", line 195, in build
    result = _build(
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/build.py", line 268, in _build
    graph = dispatch(sources, manager, stdout)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/build.py", line 2927, in dispatch
    process_graph(graph, manager)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/build.py", line 3325, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/build.py", line 3420, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/semanal_main.py", line 93, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/semanal_main.py", line 220, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/semanal_main.py", line 349, in semantic_analyze_target
    analyzer.refresh_partial(
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/semanal.py", line 600, in refresh_partial
    self.refresh_top_level(node)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/semanal.py", line 611, in refresh_top_level
    self.accept(d)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/semanal.py", line 6475, in accept
    node.accept(self)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/nodes.py", line 1141, in accept
    return visitor.visit_class_def(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/semanal.py", line 1600, in visit_class_def
    self.analyze_class(defn)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/semanal.py", line 1685, in analyze_class
    self.analyze_class_body_common(defn)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/semanal.py", line 1714, in analyze_class_body_common
    self.apply_class_plugin_hooks(defn)
  File "/path/to/project/venv/lib/python3.11/site-packages/mypy/semanal.py", line 1801, in apply_class_plugin_hooks
    hook(ClassDefContext(defn, base_expr, self))
  File "/path/to/project/venv/lib/python3.11/site-packages/sqlalchemy/ext/mypy/plugin.py", line 261, in _base_cls_hook
    decl_class.scan_declarative_assignments_and_apply_types(ctx.cls, ctx.api)
  File "/path/to/project/venv/lib/python3.11/site-packages/sqlalchemy/ext/mypy/decl_class.py", line 96, in scan_declarative_assignments_and_apply_types
    _scan_declarative_assignment_stmt(
  File "/path/to/project/venv/lib/python3.11/site-packages/sqlalchemy/ext/mypy/decl_class.py", line 459, in _scan_declarative_assignment_stmt
    python_type_for_type = infer.infer_type_from_right_hand_nameexpr(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/venv/lib/python3.11/site-packages/sqlalchemy/ext/mypy/infer.py", line 52, in infer_type_from_right_hand_nameexpr
    python_type_for_type = _infer_type_from_decl_column(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/venv/lib/python3.11/site-packages/sqlalchemy/ext/mypy/infer.py", line 410, in _infer_type_from_decl_column
    return _infer_type_from_left_and_inferred_right(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/venv/lib/python3.11/site-packages/sqlalchemy/ext/mypy/infer.py", line 457, in _infer_type_from_left_and_inferred_right
    format_type(orig_left_hand_type),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: format_type() missing 1 required positional argument: 'options'

Versions

  • OS: Ubuntu 22.04 LTS
  • Python: 3.11.3
  • SQLAlchemy: 1.4.48
  • Type checker (eg: mypy 0.991, pyright 1.1.290, etc): mypy 1.4.1 (and current master)

requirements.txt:

mypy==1.4.1
mypy-extensions==1.0.0
sqlalchemy[mypy]==1.4.48
sqlalchemy2-stubs==0.0.2a34
types-sqlalchemy==1.4.53.38
types-sqlalchemy-utils==1.0.1
typing-extensions==4.6.3

Additional context

This is a condensed minimal reproducible example. In short, Model is my abstract base model for all ORM classes. Bar is an ORM model that has an enum property.

Declaring the column as enum would be correct, but it's not necessary to trigger the bug. __abstract__ = True is missing of course. Code just serves as a minimal example and doesn't make sense. I just tried to remove all distractions.

@hofrob hofrob added requires triage New issue that requires categorization typing pep -484 typing issues. independent of "mypy" labels Jul 5, 2023
@hofrob
Copy link
Author

hofrob commented Jul 5, 2023

Originally posted in the mypy repo where @AlexWaygood commented the following:

If the bug still exists in the latest version of the sqlalchemy mypy plugin, it indicates that they need to update the plugin following python/mypy#6617 (comment)

@CaselIT
Copy link
Member

CaselIT commented Jul 5, 2023

Hi,

Can you try removing the packages types-sqlalchemy and types-sqlalchemy-utils?
Can you also reproduce a similar error using v2 (without any stub package in that case)?

@CaselIT CaselIT added awaiting info waiting for the submitter to give more information SQLA mypy plugin mypy plugin issues only. general pep-484 issues should be "typing" and removed requires triage New issue that requires categorization labels Jul 5, 2023
@zzzeek
Copy link
Member

zzzeek commented Jul 5, 2023

hi -

this is resolved for 1.4.49 not yet released in b39af62

@zzzeek zzzeek closed this as completed Jul 5, 2023
@zzzeek zzzeek removed the awaiting info waiting for the submitter to give more information label Jul 5, 2023
@zzzeek zzzeek added this to the 1.4.x milestone Jul 5, 2023
@zzzeek
Copy link
Member

zzzeek commented Jul 5, 2023

as well as 2.0.17 already released

@zzzeek
Copy link
Member

zzzeek commented Jul 5, 2023

so much for our typing error template

@hofrob
Copy link
Author

hofrob commented Jul 5, 2023

Do you know how much time I spent finding the actual bug? Sorry, that you had to spend 15s on closing this ticket. Finding and creating a reproducible piece of code is a PITA. And yes, I didn't have time to first cover all demands that mypy has on creating issues, and then do the same in this project.

And if you look at the text and title, I'm using 1.4.

@zzzeek
Copy link
Member

zzzeek commented Jul 5, 2023

Do you know how much time I spent finding the actual bug? Sorry, that you had to spend 15s on closing this ticket.

sorry, thanks very much for submitting this issue!

Re: the bug reporting template we are trying to reduce the time we have to spend understanding people's reports. We get a dozen reports a day. Unfortunately the stack trace would have been sufficient here as it points to a direct issue in how we are calling a mypy API that changed (where in fact our test suite broke, so we knew about this immediately), but you didn't know that, so, sorry about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SQLA mypy plugin mypy plugin issues only. general pep-484 issues should be "typing" typing pep -484 typing issues. independent of "mypy"
Projects
None yet
Development

No branches or pull requests

3 participants