Skip to content

Commit d6cc690

Browse files
committed
Merge remote-tracking branch 'upstream/master' into conditional-overloads
2 parents 14defb4 + 1ccefbd commit d6cc690

File tree

11 files changed

+91
-25
lines changed

11 files changed

+91
-25
lines changed

.editorconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
root = true
22

3-
[*.{py,c,cpp,h,rst,md,yml,json}]
3+
[*.{py,c,cpp,h,rst,md,yml,json,test}]
44
trim_trailing_whitespace = true
55
insert_final_newline = true
66
indent_style = space
77

8-
[*.{py,c,h,json}]
8+
[*.{py,c,h,json,test}]
99
indent_size = 4
1010

1111
[*.yml]

.github/workflows/mypy_primer_comment.yml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ jobs:
3838
fs.writeFileSync("diff.zip", Buffer.from(download.data));
3939
4040
- run: unzip diff.zip
41+
# 30000 bytes is about 300 lines, posting comment fails if too long
42+
- run: |
43+
cat diff_*.txt | head -c 30000 | tee fulldiff.txt
4144
4245
# Based on https://github.com/kanga333/comment-hider
4346
- name: Hide old comments
@@ -77,23 +80,22 @@ jobs:
7780
github-token: ${{secrets.GITHUB_TOKEN}}
7881
script: |
7982
const fs = require('fs')
80-
// Keep in sync with shards produced by mypy_primer workflow
81-
const data = (
82-
['diff_0.txt', 'diff_1.txt', 'diff_2.txt']
83-
.map(fileName => fs.readFileSync(fileName, { encoding: 'utf8' }))
84-
.join('')
85-
.substr(0, 30000) // About 300 lines
86-
)
83+
const data = fs.readFileSync('fulldiff.txt', { encoding: 'utf8' })
8784
8885
console.log("Diff from mypy_primer:")
8986
console.log(data)
9087
88+
let body
9189
if (data.trim()) {
92-
const body = 'Diff from [mypy_primer](https://github.com/hauntsaninja/mypy_primer), showing the effect of this PR on open source code:\n```diff\n' + data + '```'
93-
await github.issues.createComment({
94-
issue_number: fs.readFileSync("pr_number.txt", { encoding: "utf8" }),
95-
owner: context.repo.owner,
96-
repo: context.repo.repo,
97-
body
98-
})
90+
body = 'Diff from [mypy_primer](https://github.com/hauntsaninja/mypy_primer), showing the effect of this PR on open source code:\n```diff\n' + data + '```'
91+
} else {
92+
body = 'According to [mypy_primer](https://github.com/hauntsaninja/mypy_primer), this change has no effect on the checked open source code. 🤖🎉'
9993
}
94+
const prNumber = parseInt(fs.readFileSync("pr_number.txt", { encoding: "utf8" }))
95+
await github.issues.createComment({
96+
issue_number: prNumber,
97+
owner: context.repo.owner,
98+
repo: context.repo.repo,
99+
body
100+
})
101+
return prNumber

mypy/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def find_config_file_line_number(path: str, section: str, setting_name: str) ->
511511
in_desired_section = False
512512
try:
513513
results = []
514-
with open(path) as f:
514+
with open(path, encoding="UTF-8") as f:
515515
for i, line in enumerate(f):
516516
line = line.strip()
517517
if line.startswith('[') and line.endswith(']'):

mypy/semanal.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
CallableType, Overloaded, Instance, Type, AnyType, LiteralType, LiteralValue,
9999
TypeTranslator, TypeOfAny, TypeType, NoneType, PlaceholderType, TPDICT_NAMES, ProperType,
100100
get_proper_type, get_proper_types, TypeAliasType, TypeVarLikeType,
101-
PROTOCOL_NAMES, TYPE_ALIAS_NAMES, FINAL_TYPE_NAMES, FINAL_DECORATOR_NAMES,
101+
PROTOCOL_NAMES, TYPE_ALIAS_NAMES, FINAL_TYPE_NAMES, FINAL_DECORATOR_NAMES, REVEAL_TYPE_NAMES,
102102
is_named_instance,
103103
)
104104
from mypy.typeops import function_type, get_type_vars
@@ -2177,11 +2177,14 @@ def can_be_type_alias(self, rv: Expression, allow_none: bool = False) -> bool:
21772177
return True
21782178
if allow_none and isinstance(rv, NameExpr) and rv.fullname == 'builtins.None':
21792179
return True
2180-
if (isinstance(rv, OpExpr)
2181-
and rv.op == '|'
2182-
and self.can_be_type_alias(rv.left, allow_none=True)
2183-
and self.can_be_type_alias(rv.right, allow_none=True)):
2184-
return True
2180+
if isinstance(rv, OpExpr) and rv.op == '|':
2181+
if self.is_stub_file:
2182+
return True
2183+
if (
2184+
self.can_be_type_alias(rv.left, allow_none=True)
2185+
and self.can_be_type_alias(rv.right, allow_none=True)
2186+
):
2187+
return True
21852188
return False
21862189

21872190
def is_type_ref(self, rv: Expression, bare: bool = False) -> bool:
@@ -3874,7 +3877,7 @@ def visit_call_expr(self, expr: CallExpr) -> None:
38743877
expr.analyzed.line = expr.line
38753878
expr.analyzed.column = expr.column
38763879
expr.analyzed.accept(self)
3877-
elif refers_to_fullname(expr.callee, 'builtins.reveal_type'):
3880+
elif refers_to_fullname(expr.callee, REVEAL_TYPE_NAMES):
38783881
if not self.check_fixed_args(expr, 1, 'reveal_type'):
38793882
return
38803883
expr.analyzed = RevealExpr(kind=REVEAL_TYPE, expr=expr.args[0])

mypy/types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@
126126
'typing.Reversible',
127127
)
128128

129+
REVEAL_TYPE_NAMES: Final = (
130+
'builtins.reveal_type',
131+
'typing.reveal_type',
132+
'typing_extensions.reveal_type',
133+
)
134+
129135
# A placeholder used for Bogus[...] parameters
130136
_dummy: Final[Any] = object()
131137

mypyc/irbuild/prepare.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def prepare_method_def(ir: ClassIR, module_name: str, cdef: ClassDef, mapper: Ma
142142
ir.method_decls[PROPSET_PREFIX + node.name] = decl
143143

144144
if node.func.is_property:
145-
assert node.func.type
145+
assert node.func.type, f"Expected return type annotation for property '{node.name}'"
146146
decl.is_prop_getter = True
147147
ir.property_types[node.name] = decl.sig.ret_type
148148

test-data/unit/check-expressions.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,22 @@ main:1: note: Revealed type is "Any"
16881688
def reveal_type(x: int) -> None: pass
16891689
reveal_type("foo") # E: Argument 1 to "reveal_type" has incompatible type "str"; expected "int"
16901690

1691+
[case testTypingRevealType]
1692+
from typing import reveal_type
1693+
from typing import reveal_type as show_me_the_type
1694+
1695+
reveal_type(1) # N: Revealed type is "Literal[1]?"
1696+
show_me_the_type(1) # N: Revealed type is "Literal[1]?"
1697+
1698+
[case testTypingExtensionsRevealType]
1699+
from typing_extensions import reveal_type
1700+
from typing_extensions import reveal_type as show_me_the_type
1701+
1702+
reveal_type(1) # N: Revealed type is "Literal[1]?"
1703+
show_me_the_type(1) # N: Revealed type is "Literal[1]?"
1704+
1705+
[builtins fixtures/tuple.pyi]
1706+
16911707
[case testRevealTypeVar]
16921708
reveal_type = 1
16931709
1 + "foo" # E: Unsupported operand types for + ("int" and "str")

test-data/unit/check-union-or-syntax.test

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,30 @@ def f(x: Union[int, str, None]) -> None:
196196
else:
197197
reveal_type(x) # N: Revealed type is "None"
198198
[builtins fixtures/isinstance.pyi]
199+
200+
[case testImplicit604TypeAliasWithCyclicImportInStub]
201+
# flags: --python-version 3.10
202+
from was_builtins import foo
203+
reveal_type(foo) # N: Revealed type is "Union[builtins.str, was_mmap.mmap]"
204+
[file was_builtins.pyi]
205+
import was_mmap
206+
WriteableBuffer = was_mmap.mmap
207+
ReadableBuffer = str | WriteableBuffer
208+
foo: ReadableBuffer
209+
[file was_mmap.pyi]
210+
from was_builtins import *
211+
class mmap: ...
212+
213+
# TODO: Get this test to pass
214+
[case testImplicit604TypeAliasWithCyclicImportNotInStub-xfail]
215+
# flags: --python-version 3.10
216+
from was_builtins import foo
217+
reveal_type(foo) # N: Revealed type is "Union[builtins.str, was_mmap.mmap]"
218+
[file was_builtins.py]
219+
import was_mmap
220+
WriteableBuffer = was_mmap.mmap
221+
ReadableBuffer = str | WriteableBuffer
222+
foo: ReadableBuffer
223+
[file was_mmap.py]
224+
from was_builtins import *
225+
class mmap: ...

test-data/unit/cmdline.pyproject.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,11 @@ i: int = 0
125125
This isn't even syntatically valid!
126126
[file x/please_skipme_.py]
127127
Neither is this!
128+
129+
[case testPyprojectTOMLUnicode]
130+
# cmd: mypy x.py
131+
[file pyproject.toml]
132+
\[project]
133+
description = "Factory ⸻ A code generator 🏭"
134+
\[tool.mypy]
135+
[file x.py]

test-data/unit/lib-stub/typing.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,5 @@ class Sequence(Iterable[T_co]):
4747
class Mapping(Iterable[T], Generic[T, T_co]): pass
4848

4949
def final(meth: T) -> T: pass
50+
51+
def reveal_type(__obj: T) -> T: pass

test-data/unit/lib-stub/typing_extensions.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ class _TypedDict(Mapping[str, object]):
4444
def __delitem__(self, k: NoReturn) -> None: ...
4545

4646
def TypedDict(typename: str, fields: Dict[str, Type[_T]], *, total: Any = ...) -> Type[dict]: ...
47+
48+
def reveal_type(__obj: T) -> T: pass

0 commit comments

Comments
 (0)