Skip to content

Python 3.14.0rc1 seems to break (ALL?) sample code with KeyError: '__annotations__' runtime Exception? #902

@cljponies

Description

@cljponies

Describe the bug

I recently used Python Install Manager to install Python on a new system since that is apparently now the recommended procedure. It decided to install "Python 3.14.0rc1 (tags/v3.14.0rc1:48f8831, Jul 22 2025, 17:09:57) [MSC v.1944 64 bit (AMD64)] on win32" which I didn't think too much about (but looking at the Python docs now that is "the pre-release of the next version of the Python programming language").

I tried to run the simplest example code inside a PyCharm 2025.1.3.1 project and was confused/shocked when I hit the following exception:

(.venv) C:\Projects\APITest>python main.py
Traceback (most recent call last):
  File "C:\Projects\APITest\main.py", line 2, in <module>
    from polygon import RESTClient
  File "C:\Projects\APITest\.venv\Lib\site-packages\polygon\__init__.py", line 1, in <module>
    from .rest import RESTClient
  File "C:\Projects\APITest\.venv\Lib\site-packages\polygon\rest\__init__.py", line 1, in <module>
    from .aggs import AggsClient
  File "C:\Projects\APITest\.venv\Lib\site-packages\polygon\rest\aggs.py", line 1, in <module>
    from .base import BaseClient
  File "C:\Projects\APITest\.venv\Lib\site-packages\polygon\rest\base.py", line 10, in <module>
    from .models.request import RequestOptionBuilder
  File "C:\Projects\APITest\.venv\Lib\site-packages\polygon\rest\models\__init__.py", line 1, in <module>
    from .aggs import *
  File "C:\Projects\APITest\.venv\Lib\site-packages\polygon\rest\models\aggs.py", line 5, in <module>
    @modelclass
     ^^^^^^^^^^
  File "C:\Projects\APITest\.venv\Lib\site-packages\polygon\modelclass.py", line 13, in modelclass
    for a in cls.__dict__["__annotations__"].keys()
             ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
KeyError: '__annotations__'

Putting a breakpoint before that and evaluating in the Debugger console yielded:

>>> '__annotations__' in cls.__dict__
False
>>> cls
<class 'polygon.rest.models.aggs.Agg'>

Mystified that such an obvious error would go uncorrected in such a widely used repo, I looked at the Python Docs What's New and saw "The biggest changes to the implementation include template strings (PEP 750), deferred evaluation of annotations (PEP 649), and a new type of interpreter that uses tail calls." and said Aha!

Clearly PEP 649 and 749: deferred evaluation of annotations breaks your code.

Not only that but you don't even seem to be following the old Annotations Best Practices which would have avoided this error I think (but I don't claim to understand all the ins and outs of Annotations in Python).

Anyway I changed my .venv/Lib/site-packages/polygon/modelclass.py from

   def modelclass(cls: typing.Type[_T]) -> typing.Type[_T]:
       cls = dataclass(cls)
       attributes = [
           a
           for a in cls.__dict__["__annotations__"].keys()
           if not a.startswith("__") and not inspect.isroutine(a)
       ]

to:

   def modelclass(cls: typing.Type[_T]) -> typing.Type[_T]:
       cls = dataclass(cls)
       if isinstance(cls, type):
           ann = cls.__dict__.get('__annotations__', None)
       else:
           ann = getattr(cls, '__annotations__', None)
       if ann is None:
           attributes = []
       else:
           attributes = [
               a
               for a in ann.keys()
               if not a.startswith("__") and not inspect.isroutine(a)
               ]

and very limited testing indicates things seem to work now (although rate limiting makes it a bit harder to be sure).

To Reproduce
Install Python 3.14.0rc1
Run any sample code (but I used basically the first part of the README) using that version of Python.
Anything with the line:
from polygon import RESTClient
will throw a KeyError Exception.

Expected behavior
example runs without throwing KeyError Exception.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions