Detecting Platform Specific Dependency issues

I have a codebase that is tested to run on both mac/linux. One of dependencies only works on linux and I try to do,

if sys.platform == "linux"
  import linux_dep
  ...
else:
  ...

The ci I’m using has linux machines. Running ci job on mac is less convenient and would prefer not to setup. Is there a good way to have pyright fail if I accidentally do import linux_dep outside a guarded area? I’m aware of pyright --pythonplatform=Darwin. That when run on linux machine with linux_dep installed won’t fail though if it’s imported outside guard.

I do have some control over linux_dep’s codebase and could make changes to it if that’d help. Like I could add assert sys.platform == “linux” inside of it, but that would mostly just lead to a lot of code being marked unreachable and wouldn’t cause type checker error. Other issue is normally type checkers don’t report on errors in dependencies so while I could maybe do hacky thing of,

if sys.platform != "linux":
  "a" + 1 # any intentional type checker error

that would not I think show an error when checking the main project. It’d also be ideal bonus to do this in a way that doesn’t require changes to linux_dep as it’s not really it’s problem that another repo wants to use it and also supports mac.

Could you write a wrapper module that imports + reexports everything in linux_deps for linux, and is empty for everything else? Then you could change your codebase to import that wrapper module, and not worry about sys.platform checks everywhere.

I think that approach can work.

The usage of platform dependencies is for functionality that’s only available there. For example let’s say platform dependency offers a couple classes Foo, Bar, and Baz. My codebase if you are on Linux uses those to define some functionality. If on mac that functionality doesn’t exist and if you try to use it should fail. I’m not trying to implement a function with different logic depending on platform, but to have some functionality that’s only supported on one.

I could import this platform module and avoid doing from import so that any usage of it stays within functions/classes that need it. Even that still requires sys.platform asserts though. If I try to do platform.foo_Foo for a class only defined on Linux would give an error message of unknown function Foo on Mac. So I think sys.platform checks remain necessary and are required here. One awkward thing is sys.platform guard also needs to cover type hints. From future annotations trick isn’t enough here as while at runtime it’d be ok, at type checker time if there’s no sys check it would say the type is unknown.