@@ -399,6 +399,207 @@ B_explicit_alias
399399[builtins fixtures/tuple.pyi]
400400
401401
402+ [case testDeprecatedClassConstructorInCallableTypeContext]
403+ # flags: --enable-error-code=deprecated
404+
405+ from typing import Any, Callable, ClassVar, TypeVar, Generic
406+ from typing_extensions import deprecated
407+
408+ class A:
409+ @deprecated("do not use")
410+ def __init__(self) -> None: ...
411+
412+ def receives_callable(c: Callable[..., Any], /) -> None: ...
413+ callable_receiver: Callable[[Callable[..., Any]], None]
414+
415+ T = TypeVar("T")
416+
417+ class CallableAttr(Generic[T]):
418+ def __get__(self, instance: Settable[T], owner: type[Settable[T]], /) -> T: ...
419+ def __set__(self, instance: Settable[T], value: T, /) -> None: ...
420+
421+ class Settable(Generic[T]):
422+ instance: T
423+ attr = CallableAttr[T]()
424+
425+ @property
426+ def prop(self) -> T: ...
427+ @prop.setter
428+ def prop(self, c: T, /) -> None: ...
429+
430+ # Simple assignment
431+ A_callable: Callable[..., Any] = (
432+ A # E: function __main__.A.__init__ is deprecated: do not use
433+ )
434+
435+ # Multiple assignments
436+ A_multi_callable: Callable[..., Any]
437+ A_multi_callable, var = (
438+ A, # E: function __main__.A.__init__ is deprecated: do not use
439+ 1,
440+ )
441+
442+ # Function argument
443+ receives_callable(
444+ A # E: function __main__.A.__init__ is deprecated: do not use
445+ )
446+
447+ # Callable type argument
448+ callable_receiver(
449+ A # E: function __main__.A.__init__ is deprecated: do not use
450+ )
451+
452+ # Function return type
453+ def func_returns_callable(arg: int) -> Callable[..., Any]:
454+ return A # E: function __main__.A.__init__ is deprecated: do not use
455+
456+ # Typed lambda return type
457+ lambda_returns_callable_1: Callable[[], Callable[..., Any]] = (
458+ lambda: A # E: function __main__.A.__init__ is deprecated: do not use
459+ )
460+ lambda_returns_callable_2: Callable[[], Callable[..., Any]]
461+ lambda_returns_callable_2 = lambda: (
462+ A # E: function __main__.A.__init__ is deprecated: do not use
463+ )
464+
465+ # Class and instance attributes
466+ settable: Settable[Callable[..., Any]]
467+ settable.instance = (
468+ A # E: function __main__.A.__init__ is deprecated: do not use
469+ )
470+ settable.attr = (
471+ A # E: function __main__.A.__init__ is deprecated: do not use
472+ )
473+ settable.prop = (
474+ A # E: function __main__.A.__init__ is deprecated: do not use
475+ )
476+ class SettableChild(Settable[Callable[..., Any]]):
477+ class_: ClassVar[Callable[..., Any]] = (
478+ A # E: function __main__.A.__init__ is deprecated: do not use
479+ )
480+
481+ # Checks for false positives
482+
483+ def receives_type(t: type[A], /) -> None: ...
484+ def receives_object(o: object) -> None: ...
485+ def receives_any(o: Any) -> None: ...
486+ type_receiver: Callable[[type[A]], None]
487+ object_receiver: Callable[[object], None]
488+ any_receiver: Callable[[Any], None]
489+
490+ A_type: type[A] = A
491+ A_object: object = A
492+ A_any: Any = A
493+ receives_type(A_type)
494+ receives_object(A_object)
495+ receives_any(A_any)
496+ type_receiver(A_type)
497+ object_receiver(A_object)
498+ any_receiver(A_any)
499+
500+ def func_returns_type(arg: int) -> type[A]: return A
501+ def func_returns_object(arg: int) -> object: return A
502+ def func_returns_any(arg: int) -> Any: return A
503+ lambda_returns_type: Callable[[], type[A]] = lambda: A
504+ lambda_returns_object: Callable[[], object] = lambda: A
505+ lambda_returns_any: Callable[[], Any] = lambda: A
506+
507+ settable2: Settable[type[A]]
508+ settable2.instance = A
509+ settable2.attr = A
510+ settable2.prop = A
511+
512+ class SettableChild2(Settable[type[A]]):
513+ class_: ClassVar[type[A]] = A
514+
515+ [builtins fixtures/property.pyi]
516+
517+
518+ [case testDeprecatedClassConstructorInUnionTypeContext]
519+ # flags: --enable-error-code=deprecated
520+
521+ from typing import Any, Callable, Union, Optional
522+ from typing_extensions import deprecated
523+
524+ class Dummy: ...
525+
526+ class A:
527+ @deprecated("do not use")
528+ def __init__(self) -> None: ...
529+
530+ callable_or_dummy: Union[Callable[..., Any], Dummy] = A # E: function __main__.A.__init__ is deprecated: do not use
531+ maybe_callable: Optional[Callable[..., Any]] = A # E: function __main__.A.__init__ is deprecated: do not use
532+
533+ type_or_dummy: Union[type[A], Dummy] = A
534+ maybe_type: Optional[type[A]] = A
535+
536+ [builtins fixtures/tuple.pyi]
537+
538+
539+ [case testDeprecatedClassConstructorInProtocolTypeContext]
540+ # flags: --enable-error-code=deprecated
541+
542+ from typing import Protocol, Union
543+ from typing_extensions import deprecated
544+
545+ class CompatibleProto(Protocol):
546+ def __call__(self) -> A: ...
547+
548+ class IncompatibleProto1(Protocol):
549+ def __call__(self, a: int, /) -> A: ...
550+
551+ class IncompatibleProto2(Protocol):
552+ var: int
553+ def __call__(self) -> A: ...
554+
555+ class A:
556+ @deprecated("do not use")
557+ def __init__(self) -> None: ...
558+
559+ CallableA: Union[CompatibleProto, type[A]] = (
560+ A # E: function __main__.A.__init__ is deprecated: do not use
561+ )
562+ AType1: Union[IncompatibleProto1, type[A]] = A
563+ AType2: Union[IncompatibleProto2, type[A]] = A
564+
565+ [builtins fixtures/tuple.pyi]
566+
567+
568+ [case testDeprecatedOverloadedClassConstructorInCallableTypeContext]
569+ # flags: --enable-error-code=deprecated --disable-error-code=no-overload-impl
570+
571+ from typing import Callable, overload
572+ from typing_extensions import deprecated
573+
574+ class A:
575+ @overload
576+ @deprecated("use `make_a` instead")
577+ def __init__(self) -> None: ...
578+ @overload
579+ @deprecated("use `make_a_with_int()` instead")
580+ def __init__(self, a: int) -> None: ...
581+ @overload
582+ def __init__(self, a: str) -> None: ...
583+ @classmethod
584+ def make_a(cls) -> A: ...
585+ @classmethod
586+ def make_a_with_int(cls, a: int) -> A: ...
587+
588+ AFactory: Callable[[], A] = (
589+ A # E: overload def (self: __main__.A) of function __main__.A.__init__ is deprecated: use `make_a` instead
590+ )
591+ AFactoryWithInt: Callable[[int], A] = (
592+ A # E: overload def (self: __main__.A, a: builtins.int) of function __main__.A.__init__ is deprecated: use `make_a_with_int()` instead
593+ )
594+
595+ AFactoryWithStr: Callable[[str], A] = A
596+ IncompatibleFactory: Callable[[bytes], A] = (
597+ A # E: Incompatible types in assignment (expression has type "type[A]", variable has type "Callable[[bytes], A]")
598+ )
599+
600+ [builtins fixtures/tuple.pyi]
601+
602+
402603[case testDeprecatedSpecialMethods]
403604# flags: --enable-error-code=deprecated
404605
0 commit comments