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
Better-support mypy strict equality flag #8799
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
[build-system] | ||
requires = ["poetry>=0.12"] | ||
build_backend = "poetry.masonry.api" | ||
|
||
[tool.poetry] | ||
name = "test" | ||
version = "0.0.1" | ||
readme = "README.md" | ||
authors = [ | ||
"author@example.com" | ||
] | ||
|
||
[tool.poetry.dependencies] | ||
python = "*" | ||
|
||
[tool.pytest.ini_options] | ||
addopts = "-v -p no:warnings" | ||
|
||
[tool.mypy] | ||
plugins = "pydantic.mypy" | ||
ignore_missing_imports = true | ||
warn_return_any = true | ||
warn_unreachable = true | ||
warn_unused_configs = true | ||
follow_imports = "normal" | ||
show_column_numbers = true | ||
strict_optional = true | ||
warn_redundant_casts = true | ||
pretty = false | ||
strict = true | ||
warn_unused_ignores = true | ||
check_untyped_defs = true | ||
disallow_untyped_calls = true | ||
disallow_untyped_defs = true | ||
disallow_untyped_decorators = false | ||
strict_equality = true | ||
|
||
[tool.pydantic-mypy] | ||
init_forbid_extra = true | ||
init_typed = true | ||
warn_required_dynamic_aliases = true | ||
|
||
|
||
[[tool.mypy.overrides]] | ||
module = [ | ||
'pydantic_core.*', | ||
] | ||
follow_imports = "skip" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class User(BaseModel): | ||
username: str | ||
|
||
|
||
user = User(username='test') | ||
print(user == 'test') | ||
print(user.username == [1, 2, 3]) | ||
print(user.username == 'test') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class User(BaseModel): | ||
username: str | ||
|
||
|
||
user = User(username='test') | ||
print(user == 'test') | ||
# MYPY: error: Non-overlapping equality check (left operand type: "User", right operand type: "Literal['test']") [comparison-overlap] | ||
print(user.username == [1, 2, 3]) | ||
# MYPY: error: Non-overlapping equality check (left operand type: "str", right operand type: "List[int]") [comparison-overlap] | ||
print(user.username == 'test') |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,7 @@ def build(self) -> List[Union[Tuple[str, str], Any]]: | |
('pyproject-default.toml', 'computed_fields.py'), | ||
('pyproject-default.toml', 'with_config_decorator.py'), | ||
('pyproject-plugin-no-strict-optional.toml', 'no_strict_optional.py'), | ||
('pyproject-plugin-strict-equality.toml', 'strict_equality.py'), | ||
] | ||
) | ||
|
||
|
@@ -192,6 +193,8 @@ def test_mypy_results(config_filename: str, python_filename: str, request: pytes | |
if test_config.existing is not None: | ||
existing_output_code = test_config.existing.output_path.read_text() | ||
print(f'Comparing output with {test_config.existing.output_path}') | ||
else: | ||
print('Expecting no mypy errors') | ||
|
||
merged_output = merge_python_and_mypy_output(input_code, mypy_out) | ||
|
||
|
@@ -202,6 +205,9 @@ def test_mypy_results(config_filename: str, python_filename: str, request: pytes | |
test_config.current.output_path.parent.mkdir(parents=True, exist_ok=True) | ||
test_config.current.output_path.write_text(merged_output) | ||
else: | ||
print('**** Merged Output ****') | ||
print(merged_output) | ||
print('***********************') | ||
Comment on lines
+208
to
+210
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was helpful for debugging, and I don't see much reason not to include it |
||
assert existing_output_code is not None, 'No output file found, run `make test-mypy-update` to create it' | ||
assert merged_output == existing_output_code | ||
expected_returncode = get_expected_return_code(existing_output_code) | ||
|
@@ -279,7 +285,7 @@ def merge_python_and_mypy_output(source_code: str, mypy_output: str) -> str: | |
if not line: | ||
continue | ||
try: | ||
line_number, message = line.split(':', maxsplit=1) | ||
line_number, message = re.split(r':(?:\d+:)?', line, maxsplit=1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this is just a python 3.12 thing, but I was running into an issue where the messages had two numbers, like, |
||
merged_lines.insert(int(line_number), (f'# MYPY: {message.strip()}', True)) | ||
except ValueError: | ||
# This could happen due to lack of a ':' in `line`, or the pre-':' contents not being a number | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wasted some time confused by the lack of this message