pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

Issue with temporal-sdk that succeeded with other type checker

Open evan-sadler opened this issue 7 months ago • 2 comments

Describe the Bug

I noticed that pyrefly threw this error when mypy did not. I'm not sure if it's a false positive or an issue, but wanted to surface!

  ⎿  Error: Exit code 1                                     
     Using CPython 3.11.13                                                                                                                                                      
     Creating virtual environment at: .venv                                                                                                                                     
     Installed 12 packages in 87ms                                                                                                                                              
     ERROR Argument `type[str]` is not assignable to parameter `type_hint` with type `type[TypeVar[AnyType]]` in function                                                       
     `temporalio.converter._payload_converter.PayloadConverter.from_payload` [bad-argument-type]
       --> src/repro_payload.py:15:45                                                                                                                                           
        |                                                                                                                                                                     
     15 |     value = converter.from_payload(payload, str)                                                                                                                      
        |                                             ^^^
        |                                                                                                                                                                       
     … +2 lines (ctrl+o to see all)   

see project pyrefly-temporal-repro.zip

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

evan-sadler avatar Apr 09 '26 16:04 evan-sadler

Thanks for filing this @evan-sadler

It looks like it's a strange import related bug.

When a TypeVar is referenced via a dotted module path (e.g. pkg.types.T) in an @overload signature, pyrefly reports
invalid-type-var ("Type variable T is not in scope") on the overload, and then downstream callers get bad-argument-type and bad-return false positives.

Using a direct import (from pkg.types import T) works correctly. Both mypy and pyright accept both forms.

Root cause: Pyrefly doesn't recognize a module-qualified TypeVar (pkg.types.T) as being "in scope" when used in a type
annotation, even though it's a valid reference to a TypeVar.

Minimal reproduction (3 files + package):

pkg/init.py — empty

pkg/types.py:
from typing import TypeVar

T = TypeVar("T")

converter.py: from typing import Any, overload

import pkg.types

class Converter:
@overload
def convert(self, data: bytes) -> Any: ...

@overload
def convert(self, data: bytes, hint: type[pkg.types.T]) -> pkg.types.T: ...                                         
                                                    
def convert(self, data: bytes, hint: type | None = None) -> Any:                                                    
    return data

test.py:
from converter import Converter

def decode(data: bytes) -> str:
c = Converter()
value = c.convert(data, str)
return value

Pyrefly output (4 errors, all false positives): ERROR Type variable T is not in scope [invalid-type-var]
--> converter.py:8:42

ERROR Type variable T is not in scope [invalid-type-var]
--> converter.py:8:64

ERROR Argument type[str] is not assignable to parameter hint with type type[TypeVar[T]] in function
converter.Converter.convert [bad-argument-type]
--> test.py:5:29

ERROR Returned type TypeVar[T] is not assignable to declared return type str [bad-return] --> test.py:6:12

mypy / pyright output: 0 errors

lolpack avatar Apr 09 '26 17:04 lolpack

Fixed in 320b7e850

samwgoldman avatar Jul 15 '26 23:07 samwgoldman