<commit_before>module DuckTest.Infer.Expression where

import DuckTest.Types
import DuckTest.Internal.Common
import DuckTest.Internal.State
import DuckTest.Monad
import DuckTest.MonadHelper
import DuckTest.Internal.Format

inferTypeForExpression :: InternalState -> Expr e -> DuckTest e PyType
inferTypeForExpression state expr = do
    Trace %%! duckf "TEST => " expr
    ret <- case expr of

      (Var (Ident name pos) _) ->
          maybe' (getVariableType state name)
              (warn pos (duckf "The identifier " name " may not be defined") >> return Any)
              return

      (Call callexpr args pos) -> do
          exprType <- checkCallExpression state callexpr args pos
          case getCallType exprType of
              Just (Functional args ret) -> return ret
              _ -> return Any

      (Dot expr (Ident att _) pos) -> do
          exprType <- inferTypeForExpression state expr
          Trace %% "Infer for type | " ++ prettyText expr ++ " :: " ++ prettyType exprType
          case exprType of
              Any -> return Any
              _ -> case getAttribute att exprType of
                      Nothing -> do
                          warn pos $ duckf "The expression " expr " may have no attribute '" att "' (" expr " :: " exprType ")"
                          return Any
                      Just t -> return t
      (None _) -> return Any
      _ -> return Any

    Debug %% prettyText expr ++ " :: " ++ prettyType (stripAlpha ret)
    return $ stripAlpha ret


checkCallExpression st lhs args pos = do
    lhsType <- inferTypeForExpression st lhs<commit_msg>Paren type for expression. WTF?<commit_after>module DuckTest.Infer.Expression where

import DuckTest.Types
import DuckTest.Internal.Common
import DuckTest.Internal.State
import DuckTest.Monad
import DuckTest.MonadHelper
import DuckTest.Internal.Format

inferTypeForExpression :: InternalState -> Expr e -> DuckTest e PyType
inferTypeForExpression state expr = do
    Trace %%! duckf "TEST => " expr
    ret <- case expr of

      (Var (Ident name pos) _) ->
          maybe' (getVariableType state name)
              (warn pos (duckf "The identifier " name " may not be defined") >> return Any)
              return

      (Call callexpr args pos) -> do
          exprType <- checkCallExpression state callexpr args pos
          case getCallType exprType of
              Just (Functional args ret) -> return ret
              _ -> return Any

      (Dot expr (Ident att _) pos) -> do
          exprType <- inferTypeForExpression state expr
          Trace %% "Infer for type | " ++ prettyText expr ++ " :: " ++ prettyType exprType
          case exprType of
              Any -> return Any
              _ -> case getAttribute att exprType of
                      Nothing -> do
                          warn pos $ duckf "The expression " expr " may have no attribute '" att "' (" expr " :: " exprType ")"
                          return Any
                      Just t -> return t
      (Paren expr _) -> inferTypeForExpression state expr
      (None _) -> return Any
      _ -> return Any

    Debug %% prettyText expr ++ " :: " ++ prettyType (stripAlpha ret)
    return $ stripAlpha ret


checkCallExpression st lhs args pos = do
    lhsType <- inferTypeForExpression st lhs
{'fraction_non_alphanumeric': 0.08145896656534954, 'fraction_numerical': 0.0, 'mean_word_length': 2.0192660550458714, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7298466', 'n_tokens_mistral': 868, 'n_tokens_neox': 819, 'n_words': 312}