Skip to content

Commit

Permalink
Merge pull request #302 from jacebrowning/mypy-1.1
Browse files Browse the repository at this point in the history
Update the plugin to support mypy 1.1+
  • Loading branch information
jacebrowning authored May 27, 2023
2 parents 061254c + 51a4cd0 commit 09a43fc
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
python 3.11.2
poetry 1.4.1
poetry 1.4.2
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 2.1.2 (2023-05-27)

- Fixed the `mypy` plugin to support newer versions.

## 2.1.1 (2023-05-04)

- Fixed missing default value for `target_object` in container converters.
Expand Down
57 changes: 42 additions & 15 deletions datafiles/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import Callable, Optional

from mypy.nodes import MDEF, SymbolTableNode, Var
from mypy.nodes import MDEF, DataclassTransformSpec, SymbolTableNode, Var
from mypy.plugin import ClassDefContext, Plugin
from mypy.plugins.dataclasses import DataclassTransformer
from mypy.types import AnyType, TypeOfAny
Expand All @@ -13,26 +13,53 @@ def get_class_decorator_hook(
self, fullname: str
) -> Optional[Callable[[ClassDefContext], None]]:
if fullname.endswith(".datafile"):
return datafile_class_maker_callback
return self.datafile_class_maker_callback
return None

def datafile_class_maker_callback(self, ctx: ClassDefContext) -> None:
# Inherit all type definitions from dataclasses
spec = DataclassTransformSpec()
DataclassTransformer(ctx.cls, ctx.reason, spec, ctx.api).transform()

def datafile_class_maker_callback(ctx: ClassDefContext) -> None:
# Inherit all type definitions from dataclasses
DataclassTransformer(ctx).transform()
# Define 'objects' as a class property
var = Var("objects", AnyType(TypeOfAny.unannotated))
var.info = ctx.cls.info
var.is_property = True
ctx.cls.info.names[var.name] = SymbolTableNode(MDEF, var)

# Define 'objects' as a class propery
var = Var("objects", AnyType(TypeOfAny.unannotated))
var.info = ctx.cls.info
var.is_property = True
ctx.cls.info.names[var.name] = SymbolTableNode(MDEF, var)
# Define 'datafile' as an instance property
var = Var("datafile", AnyType(TypeOfAny.unannotated))
var.info = ctx.cls.info
var.is_property = True
ctx.cls.info.names[var.name] = SymbolTableNode(MDEF, var)

# Define 'datafile' as an instance property
var = Var("datafile", AnyType(TypeOfAny.unannotated))
var.info = ctx.cls.info
var.is_property = True
ctx.cls.info.names[var.name] = SymbolTableNode(MDEF, var)

class DatafilesPluginLegacy(Plugin):
def get_class_decorator_hook(
self, fullname: str
) -> Optional[Callable[[ClassDefContext], None]]:
if fullname.endswith(".datafile"):
return self.datafile_class_maker_callback
return None

def datafile_class_maker_callback(self, ctx: ClassDefContext) -> None:
# Inherit all type definitions from dataclasses
DataclassTransformer(ctx).transform() # type: ignore

# Define 'objects' as a class property
var = Var("objects", AnyType(TypeOfAny.unannotated))
var.info = ctx.cls.info
var.is_property = True
ctx.cls.info.names[var.name] = SymbolTableNode(MDEF, var)

# Define 'datafile' as an instance property
var = Var("datafile", AnyType(TypeOfAny.unannotated))
var.info = ctx.cls.info
var.is_property = True
ctx.cls.info.names[var.name] = SymbolTableNode(MDEF, var)


def mypy(version: str):
if version < "1.1":
return DatafilesPluginLegacy
return DatafilesPlugin
74 changes: 35 additions & 39 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "datafiles"
version = "2.1.1"
version = "2.1.2"
description = "File-based ORM for dataclasses."

license = "MIT"
Expand Down Expand Up @@ -62,7 +62,7 @@ black = "^22.1"
isort = "^5.10"

# Linters
mypy = "~0.991"
mypy = "^1.3"
pylint = "~2.15"
pydocstyle = "*"

Expand Down

0 comments on commit 09a43fc

Please sign in to comment.