Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
changhoetyng authored Oct 11, 2024
2 parents d3029ae + 46c108e commit 116ddd7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 19 deletions.
31 changes: 17 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ jobs:
toxenv: py
tox_extra_args: "-n 4"
test_mypyc: true

- name: Test suite with py313-ubuntu, mypyc-compiled
python: '3.13'
arch: x64
os: ubuntu-latest
toxenv: py
tox_extra_args: "-n 4"
test_mypyc: true

# - name: Test suite with py314-dev-ubuntu
# python: '3.14-dev'
# arch: x64
Expand All @@ -94,13 +94,16 @@ jobs:
os: macos-13
toxenv: py
tox_extra_args: "-n 3 mypyc/test/test_run.py mypyc/test/test_external.py"
- name: mypyc runtime tests with py38-debug-build-ubuntu
python: '3.8.17'
arch: x64
os: ubuntu-latest
toxenv: py
tox_extra_args: "-n 4 mypyc/test/test_run.py mypyc/test/test_external.py"
debug_build: true
# This is broken. See
# - https://github.com/python/mypy/issues/17819
# - https://github.com/python/mypy/pull/17822
# - name: mypyc runtime tests with py38-debug-build-ubuntu
# python: '3.8.17'
# arch: x64
# os: ubuntu-latest
# toxenv: py
# tox_extra_args: "-n 4 mypyc/test/test_run.py mypyc/test/test_external.py"
# debug_build: true

- name: Type check our own code (py38-ubuntu)
python: '3.8'
Expand Down Expand Up @@ -148,17 +151,17 @@ jobs:
./misc/build-debug-python.sh $PYTHONVERSION $PYTHONDIR $VENV
# TODO: does this do anything? env vars aren't passed to the next step right
source $VENV/bin/activate
- name: Latest Dev build
- name: Latest dev build
if: ${{ endsWith(matrix.python, '-dev') }}
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
build-essential gdb lcov libbz2-dev libffi-dev libgdbm-dev liblzma-dev libncurses5-dev \
libreadline6-dev libsqlite3-dev libssl-dev lzma lzma-dev tk-dev uuid-dev zlib1g-dev
git clone --depth 1 https://github.com/python/cpython.git /tmp/cpython --branch $( echo ${{ matrix.python }} | sed 's/-dev//' )
cd /tmp/cpython
echo git rev-parse HEAD; git rev-parse HEAD
git show --no-patch
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
build-essential gdb lcov libbz2-dev libffi-dev libgdbm-dev liblzma-dev libncurses5-dev \
libreadline6-dev libsqlite3-dev libssl-dev lzma lzma-dev tk-dev uuid-dev zlib1g-dev
./configure --prefix=/opt/pythondev
make -j$(nproc)
sudo make install
Expand Down Expand Up @@ -190,7 +193,7 @@ jobs:
- name: Setup tox environment
run: |
tox run -e ${{ matrix.toxenv }} --notes
tox run -e ${{ matrix.toxenv }} --notest
- name: Test
run: tox run -e ${{ matrix.toxenv }} --skip-pkg-install -- ${{ matrix.tox_extra_args }}
continue-on-error: ${{ matrix.allow_failure == 'true' }}
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ List of documentation updates:
* Make changelog visible in mypy documentation (quinn-sasha, PR [17742](https://github.com/python/mypy/pull/17742))
* List all incomplete features in `--enable-incomplete-feature` docs (sobolevn, PR [17633](https://github.com/python/mypy/pull/17633))
* Remove the explicit setting of a pygments theme (Pradyun Gedam, PR [17571](https://github.com/python/mypy/pull/17571))
* Document ReadOnly with TypedDict (Jukka Lehtosalo, PR [17905](https://github.com/python/mypy/pull/17905))
* Document TypeIs (Chelsea Durazo, PR [17821](https://github.com/python/mypy/pull/17821))

### Experimental Inline TypedDict Syntax

Expand Down Expand Up @@ -250,6 +252,9 @@ This feature was contributed by Ivan Levkivskyi (PR [17457](https://github.com/p
* Fix typechecking for async generators (Danny Yang, PR [17452](https://github.com/python/mypy/pull/17452))
* Fix strict optional handling in attrs plugin (Ivan Levkivskyi, PR [17451](https://github.com/python/mypy/pull/17451))
* Allow mixing ParamSpec and TypeVarTuple in Generic (Ivan Levkivskyi, PR [17450](https://github.com/python/mypy/pull/17450))
* Improvements to `functools.partial` of types (Shantanu, PR [17898](https://github.com/python/mypy/pull/17898))
* Make ReadOnly TypedDict items covariant (Jukka Lehtosalo, PR [17904](https://github.com/python/mypy/pull/17904))
* Fix union callees with `functools.partial` (Jukka Lehtosalo, PR [17903](https://github.com/python/mypy/pull/17903))

### Typeshed Updates

Expand All @@ -263,6 +268,7 @@ Thanks to all mypy contributors who contributed to this release:
- Bénédikt Tran
- Brian Schubert
- bzoracler
- Chelsea Durazo
- Danny Yang
- Edgar Ramírez Mondragón
- Eric Mark Martin
Expand Down
19 changes: 17 additions & 2 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
from mypy.errors import CompileError
from mypy.find_sources import InvalidSourceList, create_source_list
from mypy.fscache import FileSystemCache
from mypy.modulefinder import BuildSource, FindModuleCache, SearchPaths, get_search_dirs, mypy_path
from mypy.modulefinder import (
BuildSource,
FindModuleCache,
ModuleNotFoundReason,
SearchPaths,
get_search_dirs,
mypy_path,
)
from mypy.options import INCOMPLETE_FEATURES, BuildType, Options
from mypy.split_namespace import SplitNamespace
from mypy.version import __version__
Expand Down Expand Up @@ -1413,7 +1420,15 @@ def set_strict_flags() -> None:
fail(f"Package name '{p}' cannot have a slash in it.", stderr, options)
p_targets = cache.find_modules_recursive(p)
if not p_targets:
fail(f"Can't find package '{p}'", stderr, options)
reason = cache.find_module(p)
if reason is ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS:
fail(
f"Package '{p}' cannot be type checked due to missing py.typed marker. See https://mypy.readthedocs.io/en/stable/installed_packages.html for more details",
stderr,
options,
)
else:
fail(f"Can't find package '{p}'", stderr, options)
targets.extend(p_targets)
for m in special_opts.modules:
targets.append(BuildSource(None, m, None))
Expand Down
15 changes: 14 additions & 1 deletion mypy/plugins/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Type,
TypeOfAny,
UnboundType,
UnionType,
get_proper_type,
)

Expand Down Expand Up @@ -130,7 +131,19 @@ def partial_new_callback(ctx: mypy.plugin.FunctionContext) -> Type:
if isinstance(get_proper_type(ctx.arg_types[0][0]), Overloaded):
# TODO: handle overloads, just fall back to whatever the non-plugin code does
return ctx.default_return_type
fn_type = ctx.api.extract_callable_type(ctx.arg_types[0][0], ctx=ctx.default_return_type)
return handle_partial_with_callee(ctx, callee=ctx.arg_types[0][0])


def handle_partial_with_callee(ctx: mypy.plugin.FunctionContext, callee: Type) -> Type:
if not isinstance(ctx.api, mypy.checker.TypeChecker): # use internals
return ctx.default_return_type

if isinstance(callee_proper := get_proper_type(callee), UnionType):
return UnionType.make_union(
[handle_partial_with_callee(ctx, item) for item in callee_proper.items]
)

fn_type = ctx.api.extract_callable_type(callee, ctx=ctx.default_return_type)
if fn_type is None:
return ctx.default_return_type

Expand Down
21 changes: 19 additions & 2 deletions test-data/unit/check-functools.test
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,32 @@ fn1: Union[Callable[[int], int], Callable[[int], int]]
reveal_type(functools.partial(fn1, 2)()) # N: Revealed type is "builtins.int"

fn2: Union[Callable[[int], int], Callable[[int], str]]
reveal_type(functools.partial(fn2, 2)()) # N: Revealed type is "builtins.object"
reveal_type(functools.partial(fn2, 2)()) # N: Revealed type is "Union[builtins.int, builtins.str]"

fn3: Union[Callable[[int], int], str]
reveal_type(functools.partial(fn3, 2)()) # E: "str" not callable \
# E: "Union[Callable[[int], int], str]" not callable \
# N: Revealed type is "builtins.int" \
# E: Argument 1 to "partial" has incompatible type "Union[Callable[[int], int], str]"; expected "Callable[..., int]"
[builtins fixtures/tuple.pyi]

[case testFunctoolsPartialUnionOfTypeAndCallable]
import functools
from typing import Callable, Union, Type
from typing_extensions import TypeAlias

class FooBar:
def __init__(self, arg1: str) -> None:
pass

def f1(t: Union[Type[FooBar], Callable[..., 'FooBar']]) -> None:
val = functools.partial(t)

FooBarFunc: TypeAlias = Callable[..., 'FooBar']

def f2(t: Union[Type[FooBar], FooBarFunc]) -> None:
val = functools.partial(t)
[builtins fixtures/tuple.pyi]

[case testFunctoolsPartialExplicitType]
from functools import partial
from typing import Type, TypeVar, Callable
Expand Down

0 comments on commit 116ddd7

Please sign in to comment.